在com中用CSocket类Create出现错误

//debug mfc静态链接
在Com的一个方法中用CSocket类
CSocket  proxySK;
proxySK.create();  //这一句会出错

解决方法:
在函数的开头加上以下代码来在CSocket所在的Thread中初始化CSocket和SOCKET做映射的链表:
#define _afxSockThreadState AfxGetModuleThreadState() 
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE 
 
 _AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState; 
      if (pState->m_pmapSocketHandle == NULL) 
         pState->m_pmapSocketHandle = new CMapPtrToPtr; 
      if (pState->m_pmapDeadSockets == NULL) 
         pState->m_pmapDeadSockets = new CMapPtrToPtr; 
      if (pState->m_plistSocketNotifications == NULL) 
         pState->m_plistSocketNotifications = new CPtrList;

据说是一个Bug

原文是这样说的
Hi,

When using MFC sockets in secondary threads in a statically linked MFC
Visual C++ 6.0 application, an unhandled exception occurs. The reason for
the unhandled exception is that an object of type CMapPtrToPtr pointer,
pointed to by m_pmapSocketHandle, is never created.
To resolve it the handle maps used by the sockets need to be created for
each thread.The following code shows a function to do this:
void SocketThreadInit()
{
#ifndef _AFXDLL
#define _AFX_SOCK_THREAD_STATE AFX_MODULE_THREAD_STATE
#define _afxSockThreadState AfxGetModuleThreadState()

_AFX_SOCK_THREAD_STATE* pState = _afxSockThreadState;
if (pState->m_pmapSocketHandle == NULL)
pState->m_pmapSocketHandle = new CMapPtrToPtr;
if (pState->m_pmapDeadSockets == NULL)
pState->m_pmapDeadSockets = new CMapPtrToPtr;
if (pState->m_plistSocketNotifications == NULL)
pState->m_plistSocketNotifications = new CPtrList;

#endif
}
This function should be called once in each secondary thread before the
first socket is created in the new thread.

IMP : This bug was corrected in Visual Studio 6.0 Service Pack 3

Please refer to Q193101 from MSDN

你可能感兴趣的:(thread,exception,socket,null,mfc,Sockets)