PRB: Assertion Failed - SOCKCORE.CPP, Line 51

ID: Q130944


The information in this article applies to:


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:

Q122307 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 query words: 1.52 2.52


Keywords          : kberrmsg kb16bitonly kbMFC kbVC kbWinsock 
Version           : 1.52
Platform          : WINDOWS 
Issue type        : 

Last Reviewed: August 2, 1999