Microsoft KB Archive/196836

= The MFC socket functions may fail and return an error when you call MFC socket functions from ExitInstance of a DLL =

Article ID: 196836

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 4.0 Standard Edition

 Microsoft Visual C++ 4.1 Subscription

 Microsoft Visual C++ 4.2 Enterprise Edition

 Microsoft Visual C++ 4.2 Professional Edition</li></ul>

 Microsoft Visual C++ 5.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 5.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Enterprise Edition</li></ul>

 Microsoft Visual C++ 6.0 Professional Edition</li></ul>

 Microsoft Visual C++ 6.0 Standard Edition</li></ul> </li></ul>

-

<div class="notice_section">

This article was previously published under Q196836

<div class="symptoms_section">

SYMPTOMS
When you call MFC socket functions from ExitInstance of a DLL, the functions may fail and return an error.

<div class="cause_section">

CAUSE
In an MFC DLL, the CWinApp::ExitInstance function is called from the DllMain function in response to the DLL_PROCESS_DETACH case.

The MFC socket functions call the WinSock APIs implemented in wsock32.dll. When an application shuts down, the DllMain with DLL_PROCESS_DETACH of the WinSock DLL may be called before the DllMain of the MFC regular DLL. This can cause some process specific information to be lost.

NOTE: This problem applies to non-MFC DLLs and non-MFC sockets as well.

<div class="resolution_section">

RESOLUTION
Do not call socket functions from CWinApp::ExitInstance (DLL_PROCESS_DETACH). Provide a cleanup function that the application can call to perform the last socket functions.

<div class="status_section">

STATUS
This behavior is by design.

<div class="moreinformation_section">

MORE INFORMATION
A common mistake is to call the CSocket::Close (or CAsyncSocket::Close) function in CWinApp::ExitInstance. This can generate an assertion in sockcore.cpp line 856 in Microsoft Visual C++ 5.0, and line 667 in Microsoft Visual C++ 6.0.

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Adam Kim, Microsoft Corporation.

Keywords: kbtshoot kbdll kbprb kbwinsock KB196836

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.