静态MFC CSocket CAsyncSocket map_pp.cpp错误

静态MFC,在线程中使用SOCKET,发生错误:

Debug Assertion Failed!
Program:...
File:f:\rtm\vctools\vc7libs\ship\atlmfc\src\mfc\map_pp.cpp
Line:179

这是MFC的一个BUG,在6.0中就发现,可惜微软到VS2013还是没有修改,估计这个东西微软也要淘汰了,懒得改了,下面是MSDN的解决方法:

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 

 

最后这句就是屁话了,我在2013中试了,还是错误,可恶!

posted @ 2015-03-11 11:59  邵学军  阅读(827)  评论(0编辑  收藏  举报