Microsoft KB Archive/192102

= FIX: Resource/Memory Leaks When Using MFC in a Static Library =

Article ID: 192102

Article Last Modified on 11/21/2006

-

APPLIES TO

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

 Microsoft Visual C++ 4.2 Professional Edition

 Microsoft Visual C++ 5.0 Enterprise Edition

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

-

<div class="notice_section">

This article was previously published under Q192102

<div class="symptoms_section">

SYMPTOMS
When you run an application that is built using MFC in a static library under NuMega's BoundsChecker or other utility designed to find resource and memory leaks, the following five leaks occur:

E:\MSDEV\MFC\SRC\afxtls.cpp(180): Memory leak; 256 bytes allocated by

GlobalAlloc in afxtls.cpp at line 180, HANDLE: 0x02850004

E:\MSDEV\MFC\SRC\afxtls.cpp(122): Resource leak: Allocated by TlsAlloc

in afxtls.cpp at line 122, HANDLE: 0x0000000A

E:\MSDEV\MFC\SRC\afxtls.cpp(126): Resource leak: Allocated by

InitializeCriticalSection in afxtls.cpp at line 126, HANDLE: 0x005045D4

E:\MSDEV\MFC\SRC\afxcrit.cpp(54): Resource leak: Allocated by

InitializeCriticalSection in afxcrit.cpp at line 52, HANDLE: 0x005047E0

E:\MSDEV\MFC\SRC\afxcrit.cpp(105): Resource leak: Allocated by

InitializeCriticalSection in afxcrit.cpp at line 103, HANDLE: 0x00504778

This problem does not occur for MFC DLLs that use MFC in a static library or MFC EXEs/DLLs that use the MFC shared DLL.

<div class="cause_section">

CAUSE
MFC is allocating these objects to implement Thread Local Storage (TLS) data for it's primary thread and to synchronize access to MFC global data and resources. MFC does not free these objects when the application exits.

<div class="resolution_section">

RESOLUTION
Because these objects are allocated only once per process and should be freed by the operating system when the process terminates, you can safely ignore them.

You can also free these objects by calling AfxTermLocalData, AfxCriticalTerm, and AfxTlsRelease in the termination code. The sample code below shows this method.

<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 bug has been fixed in Visual C++ 6.0 and later versions.

<div class="moreinformation_section">

MORE INFORMATION
The following sample code demonstrates how to work around this problem. You should place the code in a separate source file and then add it to the project.

Sample Code
#include "stdafx.h"  #ifndef _AFXDLL

void AFXAPI AfxCriticalTerm;

class _AFX_TERM_EXE_STATE {  public: _AFX_TERM_EXE_STATE; ~_AFX_TERM_EXE_STATE; };

_AFX_TERM_EXE_STATE::_AFX_TERM_EXE_STATE {     // Add a reference to Thread Local Storage data. AfxTlsAddRef; }

_AFX_TERM_EXE_STATE::~_AFX_TERM_EXE_STATE {     // Terminate local data and critical sections. AfxTermLocalData(NULL, TRUE); AfxCriticalTerm;

// Release the reference to Thread Local Storage data. AfxTlsRelease; }

// Force initialization early. #pragma warning(disable: 4074) #pragma warning(disable: 4073) #pragma init_seg(lib)

_AFX_TERM_EXE_STATE _afxTermExeState; #endif // !_AFXDLL (c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Kelly Marie Ward, Microsoft Corporation.

Keywords: kb3rdparty kbbug kbcode kbfix kbvc600fix KB192102

-

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

© Microsoft Corporation. All rights reserved.