PRB: Assertion Failed - SOCKCORE.CPP, Line 51

Last reviewed: July 18, 1997
Article ID: Q130944
1.52 WINDOWS kbprg kberrmsg kbprb kbtshoot

The information in this article applies to:

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

SYMPTOMS

An application using MFC's socket classes (CSocket and CAsyncSocket) causes an assertion failure upon exit. The assertion failure text is similar to this:

   myapp Windows Application: File sockcore.cpp, Line 51, Assertion Failed!

CAUSE

This assertion failure most likely occurs if any CSocket or CAsyncSocket objects have not been closed (or destroyed).

RESOLUTION

Ensure that all socket objects in the application are properly closed and destroyed. If a socket object has been used but was not destroyed, this message will occur.

Here is a technique you can use to determine which socket object was not destroyed:

  1. Make sure that tracing has been enabled by running the "MFC Trace Options" program, which can be found in the Visual C++ group. Select the Enable Tracing check box.

  2. Debug the application using the Visual Workbench Debugger:

    Select Debug.Go

  3. When the Assertion Failed! message box appears for this assertion, select Ignore.

At this point, if you have not destroyed all of your socket objects, the Visual Workbench Output window will probably list a memory leak for the particular socket object that has not been destroyed. The message looks similar to this:

   Detected memory leaks!
   Dumping objects ->
   {2} a CSocket at $DEF0932 m_hSocket = 0x2
   m_pbBlocking = $0
   m_nConnectError = -1
   Object dump complete.

STATUS

This behavior is by design.

MORE INFORMATION

For more information on how to track down memory leaks, please see the following article in the Microsoft Knowledge Base:

   ARTICLE-ID: Q122307
   TITLE     : Tracking Down Memory Leaks with _afxBreakAlloc

Using the techniques shown in Q122307, you can determine where the socket object was allocated.

Sockets require a window to receive notification messages when socket events occur (when data is ready to be received on the socket). MFC manages this notification window for you when you use the MFC socket classes.

This window is created by MFC when your application uses a socket object. When all of the socket objects have been closed, MFC destroys the notification window.

The assertion failure message is generated by the following line:

   ASSERT(_afxSockState->hSocketWindow == NULL);

This assertion is verifying that the socket notification window has been destroyed. Because this window is destroyed when all of the socket objects have been destroyed, you have most likely created and used a socket object but never destroyed it.

A common scenario where this might occur is with a server application that opens a listening socket, and the listening socket is left open throughout the execution of the application. In this scenario, you may easily overlook the need to destroy the socket object. For example:

   BOOL CMyApp::InitInstance()
   {
      // ...
      m_pSock = new CListeningSocket;
      m_pSock->Create(nPort);
      m_pSock->Listen();
      // ...
      return CWinApp::InitInstance();
   }

This is a common sequence of events, but you must remember to destroy the socket object before exiting the application. For example:

   int CMyApp::ExitInstance()
   {
      delete m_pSock;
      return CWinapp::ExitInstance();
   }


Additional reference words: 1.52 2.52
KBCategory: kbprg kberrmsg kbprb kbtshoot
KBSubcategory: MfcSockets
Keywords : kb16bitonly MfcSockets kberrmsg kbprb kbprg kbtshoot
Technology : kbMfc
Version : 1.52
Platform : WINDOWS


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: July 18, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.