FIX: Resource/Memory Leaks When Using MFC in a Static Library (192102)
The information in this article applies to:
- The Microsoft Foundation Classes (MFC), when used with:
- Microsoft Visual C++, 32-bit Enterprise Edition 4.2
- Microsoft Visual C++, 32-bit Professional Edition 4.2
- Microsoft Visual C++, 32-bit Enterprise Edition 5.0
- Microsoft Visual C++, 32-bit Professional Edition 5.0
This article was previously published under Q192102 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.
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.
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.
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.
Modification Type: | Major | Last Reviewed: | 12/8/2003 |
---|
Keywords: | kb3rdparty kbBug kbcode kbfix kbVC600fix KB192102 |
---|
|