Foundation Class Debug Library Detects Memory LeaksID: Q99022
|
Version 2.0 of the Microsoft Foundation Classes debugging libraries
provide automatic memory leak detection, a new feature that version
1.0 did not provide.
In an application developed with a Microsoft Foundation Classes
version 1.0 library, the application object code typically calls the
Checkpoint() function in CMemoryState objects in its InitInstance()
and ExitInstance() functions. Then the application calls the
CMemoryState::Difference() function to verify that no memory leaked
during execution. This process is not necessary when the application
uses library version 2.0.
The Microsoft Foundation Classes version 2.0 debugging libraries
automatically perform memory leak detection. The detection code is in
AFXMEM.CPP, which is installed in the C:\MSVC\MFC\SRC directory by
default. This code detects the case in which an application
dynamically allocates an object and fails to delete the object before
the program terminates. The library sends a TRACE() message to the
debug window stating that memory has leaked. To see these messages,
TRACE() output must be enabled. Please refer to MFC Tech Note #7 for
more information about enabling TRACE() output.
In version 2.0, placing a CMemoryState::Checkpoint() call in
InitInstance() and ExitInstance(), as demonstrated in the code example
below, does not function correctly. The code example below produces
the following output:
Memory leaked
0 bytes in 0 Free Blocks
0 bytes in 0 Object Blocks
-56 bytes in -4 Non-Object Blocks
The negative numbers indicate that memory is allocated before the
Checkpoint() call in InitInstance() and freed before the Checkpoint()
call in ExitInstance().
-112 bytes in -4 Non-Object blocksIn the Microsoft Foundation Classes Library source code file WINHAND.CPP, line 75 calls InitHashTable(). The code is as follows:
m_temporaryMap.InitHashTable(7); // small table for temporary map
The code calls this function for each CHandleMap object: _afxMapHDC,
_afxMapHGDIOBJ, _afxMapHMENU, and _afxMapHWND. These hash tables
contain the permanent and temporary maps for window handles and their
associated C++ objects.
/*
* Compile options needed: None
*/
static CMemoryState oldstate, newstate, diffstate;
BOOL CTheApp::InitInstance()
{
// Remove the comment indication from the following 4 lines to
// delete the temporary maps and allow the code to detect true
// memory leaks.
// CGdiObject::DeleteTempMap();
// CDC::DeleteTempMap();
// CMenu::DeleteTempMap();
// CWnd::DeleteTempMap();
oldstate.Checkpoint();
m_pMainWnd = new CMainWindow();
m_pMainWnd->ShowWindow( m_nCmdShow );
m_pMainWnd->UpdateWindow();
return TRUE;
}
int CTheApp::ExitInstance()
{
newstate.Checkpoint();
if (diffstate.Difference(oldstate, newstate))
{
TRACE("Memory leaked\n");
diffstate.DumpStatistics();
}
return 0;
}
Additional query words: kbinf 1.00 1.50 1.51 1.52 2.00 2.50 2.51 2.52 noupdate
Keywords : kbnokeyword kbMFC kbVC
Version : 1.00 1.50 1.51 1.52 | 1.00
Platform : NT WINDOWS
Issue type :
Last Reviewed: July 29, 1999