用Share Liabrary没错,但只要是用了Static Library就会出错,是一样的错误。
1.m_Socket.Create();
2.CAsyncSocket::Create(nSocketPort, nSocketType, FD_READ | FD_WRITE | FD_OOB | FD_ACCEPT | FD_CONNECT | FD_CLOSE, lpszSocketAddress);
3.if (Socket(nSocketType, lEvent)) { ... }
4.CAsyncSocket::AttachHandle(m_hSocket, this, FALSE);
5.ASSERT(CAsyncSocket::LookupHandle(hSocket, bDead) == NULL);
6.pSocket = (CAsyncSocket*) pState-> m_pmapSocketHandle-> GetValueAt((void*)hSocket);
7.if (m_pHashTable == NULL) return NULL;
第7句出错。
http://support.microsoft.com/default.aspx?scid=KB;EN-US;Q193101&
FIX: Unhandled Exception Using MFC Sockets in Visual C++ 6.0
The information in this article applies to:
The Microsoft Foundation Classes (MFC), when used with:
Microsoft Visual C++, 32-bit Enterprise Edition 6.0
Microsoft Visual C++, 32-bit Professional Edition 6.0
Microsoft Visual C++, 32-bit Learning Edition 6.0
Symptoms
When using MFC sockets in secondary threads in a statically linked MFC Visual C++ 6.0 application, an unhandled exception occurs.
Cause
The reason for the unhandled exception is that an object of type CMapPtrToPtr pointer, pointed to by m_pmapSocketHandle, is never created.
Resolution
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.
SocketThreadInit,为每个线程都分配一个全局数组之类的来管理CSocket对象与socket句柄
解决办法:
一,在每个socket Create之前调用SocketThreadInit
二,主线程必须调用AfxSocketInit