FIX: MFC Sockets Application Causes GP Fault on Exit

Last reviewed: September 18, 1997
Article ID: Q130653
1.52 WINDOWS kbprg kbfixlist kbbuglist

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with: Microsoft Visual C++ for Windows, version 1.52

SYMPTOMS

An MFC application that uses MFC's socket classes (CSocket and CAsyncSocket) and statically links with MFC causes a general protection (GP) fault on exit. The GP fault occurs on the following line in SOCKCORE.CPP:

SOCKCORE.CPP(32):

   if (_afxSockState->lpfnSockCleanup != NULL)

This error occurs if the application contains a call to AfxSocketInit but for some reason terminates without ever actually calling that function. It will also occur if the call to AfxSocketInit fails.

CAUSE

The MFC socket implementation maintains a static pointer to a structure that maintains socket state information. This variable is defined in SOCKCORE.CPP as:

   static AFX_SOCKSTATE* _afxSockState;

_afxSockState is initialized when an application calls AfxSocketInit. However if an application does not call AfxSocketInit or the call to AfxSocketInit fails, the variable is not initialized, and the 'if' statement shown above attempts to remove the reference to a NULL pointer, which causes the GP fault.

RESOLUTION

  1. This problem occurs only if your application statically links with MFC. If your application uses the DLL version of MFC (MFC250(D).DLL), this problem does not occur. To learn how to use the DLL version of MFC, please see MFC TechNote #33. In particular, examine the section titled:

          "Writing An Application that Uses the DLL Version"
    

    NOTE: In order to use the DLL version of MFC with a sockets app, you also need to link with MFCN250(D), which contains the definitions for the CSocket and CAsyncSocket members.

  2. Force the application to call AfxSocketInit. This causes the variable to be properly initialized when socket support is available. However the problem still occurs if AfxSocketInit fails.

  3. If your application links statically to MFC, you can rebuild the static MFC library with a fix to the problem. To fix the problem, change the following code located on line 32 of SOCKCORE.CPP in the MFC\SRC directory:

    Change this line:

          if (_afxSockState->lpfnSockCleanup != NULL)
    

    To this line:

          if((_afxSockState!=NULL) && (_afxSockState->lpfnSockCleanup!=NULL))
    

    Once the change has been made, you can rebuild the MFC static library to incorporate the change. For details on how to rebuild the MFC static library, please see the README.TXT file in the MFC\SRC directory and Appendix B of the Class Library User's Guide.

    IMPORTANT: Remember that the problem only exists in the static library, so you should not rebuild the MFC DLL (MFC250(D).DLL).

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This problem was corrected in MFC version 2.53 included with Microsoft Visual C++ version 1.52b for Windows.


Additional reference words: 1.52 2.52 GPF
KBCategory: kbprg kbfixlist kbbuglist
KBSubcategory: MfcSockets
Keywords : kb16bitonly MfcSockets kbbuglist kbfixlist kbprg
Technology : kbMfc
Version : 1.52
Platform : WINDOWS
Solution Type : kbfix


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: September 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.