Microsoft KB Archive/139693

= FIX: Assertion Failed, Line 505 of Sockcore.cpp =

Article ID: 139693

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Visual C++ 1.52 Professional Edition

 Microsoft Visual C++ 2.1

 Microsoft Visual C++ 2.2 

-

<div class="notice_section">

This article was previously published under Q139693

<div class="symptoms_section">

SYMPTOMS
An MFC application that uses the socket classes fails with a message similar to the following:

In MFC 3.1 or 3.2, the assertion appears as:

Assertion Failed: : File sockcore.cpp, Line 505

In MFC 2.52, the assertion appears as:

Assertion Failed: : File sockcore.cpp, Line 484

<div class="cause_section">

CAUSE
When all sockets are closed, the socket handle maps are emptied and the socket notification window is destroyed.

MFC maintains an auxiliary queue of socket notification messages. If any messages remain in this queue when the last socket is closed, then this assertion failure will occur when any new sockets are opened and an attempt is made to process these left-over notifications.

<div class="resolution_section">

RESOLUTION
Purge the auxiliary queue of all messages when the last socket is closed. In this context, last does not mean previous; it means the socket that was closed, leaving no sockets open for that thread.

This problem can be remedied by overriding the CAsyncSocket::Close member function in your CAsyncSocket-derived or CSocket-derived class. The following implementation of this override will take care of the problem: void CMySocket::Close { // If Deriving from CSocket, then use: CSocket::Close; // Otherwise, use: // CAsyncSocket::Close; AFX_THREAD_STATE* pThreadState = AfxGetThreadState; if (pThreadState->m_mapSocketHandle.IsEmpty) {  // **** LAST SOCKET **** while (!pThreadState->m_listSocketNotifications.IsEmpty) delete pThreadState->m_listSocketNotifications.RemoveHead; pThreadState->m_listSocketNotifications.RemoveAll; } } To handle the case where a socket object is deleted before Close is called, also override the virtual destructor to make sure the correct version of Close is called: CMySocket::~CMySocket { if (m_hSocket != INVALID_SOCKET) Close; }

<div class="status_section">

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 the version of MFC that ships with Microsoft Visual C++, 32-bit Edition, version 4.0.

Additional query words: 1.52 2.10 2.20 2.52 3.10 3.20 4.00 CSocket CAsyncSocket

Keywords: kbbug kbfix kbnoupdate kbvc400fix kbwinsock KB139693

-

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

© Microsoft Corporation. All rights reserved.