AfxSocketInit()也调用了,但无论是Debug还是Release,用Share Liabrary没错,但只要是用了Static Library就会出错,是一样的错误。我用CreateThread创建一个线程,然后再在此线程中CSocket的Create,在Release版本中出错,代码如下:
     if(m_Socket.m_hSocket != INVALID_SOCKET)
m_Socket.Close();
      m_Socket.Create();后来发现是多线程的问题,在线程函数内加了AfxSocketInit()也还是不行。如果不是在新建的线程中调用CSocket的Create,而是在主线程,Static Library也没错。于是我用Debug来调试,一直跟踪,得到如下的调用:
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句就出错了。这句的代码是在CMapPtrToPtr的GetValueAt(void* key)方法中的,这似乎是MFC里面的保存对象与句柄的映射关系的全局数组。天!我对MFC的架构不熟,深入浅出MFC也没看,这问题简直难煞我了,还望大哥大姐们(小弟还没毕业)搭救。这问题已问了两次了,这次是为了把文字整理好看点,希望能有更多的人帮帮忙,解决者两边都可以加分。

解决方案 »

  1.   

    如果你在除主线程以外的线程中使用了CSocket,Release版本下可能无法运行,但Debug版本没问题。
      

  2.   

    在新的线程中也要调用AfxSocketInit(),不然在release中无法正常运行
      

  3.   

    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. 
      

  4.   

    在你的非主线程的开始执行
    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
       }