Article ID: 193101 - View products that this article applies to.
This article was previously published under Q193101
This article has been archived. It is offered "as is" and will no longer be updated.
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.
The handle maps used by the sockets need to be created for each thread. The following code shows a function to do this:
This function should be called once in each secondary thread before the first socket is created in the new thread.
Microsoft 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:
(https://support.microsoft.com/kb/194022/ )Visual Studio 6.0 service packs, what, where, why
(https://support.microsoft.com/kb/194295/ )How to tell that a Visual Studio service pack is installed
In MFC socket applications, AfxSocketInit is called once, by default, in the primary thread. This function creates the handle maps for the primary thread when statically linked to MFC. However, when a secondary thread is created, these per-thread handle maps are not created. AfxSocketInit must be called in each thread to initialize the socket libraries.
Steps to reproduce the behavior
For more information, click the following article number to view the article in the Microsoft Knowledge Base:
(https://support.microsoft.com/kb/175668/ )MultiSoc: Illustrates using sockets in multiple threads
Article ID: 193101 - Last Review: February 21, 2014 - Revision: 4.0