FIX: An unhandled exception occurs when you use MFC sockets in secondary threads in an MFC Visual C++ 6.0 application (193101)
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
This article was previously published under Q193101 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. STATUSMicrosoft has confirmed that this is a bug in the Microsoft products that are listed in the "Applies to" section.
This bug was corrected in Visual Studio 6.0 Service Pack 3.
You will need to call AfxSocketInit() in each thread
that uses sockets.
For more information about Visual Studio service packs, click the following article numberw to view the articlew in the Microsoft Knowledge Base:
194022
Visual Studio 6.0 service packs, what, where, why
194295 How to tell that a Visual Studio service pack is installed
REFERENCES
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
175668
MultiSoc: Illustrates using sockets in multiple threads
Modification Type: | Major | Last Reviewed: | 6/1/2005 |
---|
Keywords: | kbtshoot kbBug kbfix kbNoUpdate kbVS600sp3fix kbWinsock KB193101 kbAudDeveloper |
---|
|