这段代码的异常是什么!一个全局实例,调用时会出现异常

这段代码的错误是什么!一个全局实例,调用时会出现错误下面是一个C++全局实例代码,不知错误在哪里!C/C++ co

这段代码的错误是什么!一个全局实例,调用时会出现错误
下面是一个C++全局实例代码,不知错误在哪里!

C/C++ code
#ifndef _OUT_LIB_H_#define _OUT_LIB_H_//#include "stdafx.h"#include "IoHandle.h"#include "Session.h"#include "Network.h"#include "RecvBuf.h"#include "SendBuf.h"#include "MyObject.h"//class AsioNetwork;class MeNetObject;//class IoHandle;//class RecvBuffer;//class SendBuffer;//class Session;namespace ASIO_NET{    class COutLib    {    public:        COutLib();        ~COutLib();        static COutLib    *    Instance();        AsioNetwork        *    InstanceNetWork();        NetObject        *    InstanceNetObject();        IoHandle        *    InstanceIoHandle();        Session            *    InstanceSession();        RecvBuffer        *    InstanceRecvBuffer();        SendBuffer        *    InstanceSendBuffer();            private:        static COutLib    *    g_pInstance;        AsioNetwork        *    g_pNetWorkInstance;        MeObject        *    g_pNetObjectInstance;        IoHandle        *    g_pIoHandleInstance;        Session            *    g_pSessionInstance;        RecvBuffer        *    g_pRecvBufferInstance;        SendBuffer        *    g_pSendBufferInstance;            };    /***************************************************************************************************************************/    COutLib* COutLib::g_pInstance = 0;    COutLib::COutLib()    {        //    g_pInstance = new COutLib();            g_pNetWorkInstance = new AsioNetwork();            g_pNetObjectInstance = new MeObject();            g_pIoHandleInstance = new IoHandle();            g_pSessionInstance = new Session(1,g_pIoHandleInstance);            g_pRecvBufferInstance = new RecvBuffer();            g_pSendBufferInstance = new SendBuffer();                }    COutLib::~COutLib()    {        if(g_pInstance != NULL)        {        //    delete g_pInstance;            delete g_pNetWorkInstance;            delete g_pNetObjectInstance;            delete g_pIoHandleInstance;            delete g_pSessionInstance;            delete g_pRecvBufferInstance;            delete g_pSendBufferInstance;                    }    }    /***************************************************************************************************************************/    inline    COutLib*    COutLib::Instance()    {        return g_pInstance;    }    /***************************************************************************************************************************/    inline    AsioNetwork*    COutLib::InstanceNetWork()    {        return g_pNetWorkInstance;    }    ///***************************************************************************************************************************/    inline    NetObject*    COutLib::InstanceNetObject()    {        return g_pNetObjectInstance;    }    ///***************************************************************************************************************************/    inline    IoHandle*    COutLib::InstanceIoHandle()    {        return g_pIoHandleInstance;    }    ///***************************************************************************************************************************/    inline    RecvBuffer*    COutLib::InstanceRecvBuffer()    {        return g_pRecvBufferInstance;    }    ///***************************************************************************************************************************/    inline    SendBuffer*    COutLib::InstanceSendBuffer()    {        return g_pSendBufferInstance;    }    ///***************************************************************************************************************************/    inline    Session*    COutLib::InstanceSession()    {        return g_pSessionInstance;    }    ///***************************************************************************************************************************/}#endif 



下面是C++ 输出dll原型

C/C++ code
//声明extern DLLAPI bool //__stdcall Asio_Init( unsigned int max_client, unsigned int max_sendbuf, unsigned int max_recvbuf,     unsigned int max_client_pack, unsigned int ioThreads, unsigned int preAcceptAmount );//实现AsioNetwork ank;//使用这个声明在下面调用就没有错误!!!////Asio_Init//封装的网络模块bool  //__stdcall Asio_Init(unsigned int max_client ,unsigned int max_sendbuf ,unsigned int max_recvbuf ,unsigned int max_client_pack ,unsigned int ioThreads ,unsigned int preAcceptAmount){    AsioNetwork::Config config;    config.nStartId = 1; //Client开始ID    config.nMaxClient = max_client;    config.nMaxSendBuf = max_sendbuf;    config.nMaxRecvBuf = max_recvbuf;    config.nMaxClientPack = max_client_pack;    config.nIoThreads = ioThreads;    config.nTimeOut = 0;    config.nPreAcceptAmount = preAcceptAmount;//连接总数    config.fnCreateNetObject = Fn_CallBackCreateAcceptedObject;    config.fnDestroyNetObject = Fn_CallBackDestroyAcceptedObject;        bool bInit;//如果用下面这一行输出的DLL,在C#中运行会出现错误提示    //bInit = COutLib::Instance()->InstanceNetWork()->StartListen("127.0.0.1", 9998);//如果换成下面的直接调用就不会出错,但是我想用上面的全局实例调用DLL中的所有函数,请教代码到底哪里有问题!    ank.StartListen("127.0.0.1", 9998);    printf("Asio_Init dll !");  //  system("pause");    return bInit;}


下面是C#的调用:
C# code
public const string DLL_NAME = "AsioNetwork.dll";////Asio_Init[DllImport(DLL_NAME, CharSet = CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]public static extern bool Asio_Init(uint max_client, uint max_sendbuf, uint max_recvbuf,                                     uint max_client_pack, uint ioThreads, uint preAcceptAmount);... Asio_Init(5,32,32,16,5,1);//   这一行调用DLL出错。

运行提示的错误:c# dll尝试读取或写入受保护的内存。这通常指示其他内存已损坏





[解决办法]
溢出了?
[解决办法]
很明显,你的g_pInstance为分配内存,还是个空指针。

COutLib* COutLib::g_pInstance = 0;
改为
COutLib* COutLib::g_pInstance = new COutLib;

或者将
COutLib::Instance()
{
return g_pInstance;
}
改为
COutLib::Instance()
{
if ( NULL == g_pInstance )
g_pInstance = new COutLib;
return g_pInstance;
}

记住,最后要释放它,否则会有内存泄露