This documentation is archived and is not being maintained.


This function validates the free memory pool and prints error messages as required.

BOOL AfxCheckMemory( );

Return Value

Nonzero if no memory errors; otherwise 0.


If the function detects no memory corruption, it prints nothing.

All memory blocks currently allocated on the heap are checked, including those allocated by new but not those allocated by direct calls to underlying memory allocators, such as the malloc function or the GlobalAlloc Windows function. If any block is found to be corrupted, a message is printed to the debugger output.

If you include the line

#define new DEBUG_NEW

in a program module, then subsequent calls to AfxCheckMemory show the filename and line number where the memory was allocated.

Note   If your module contains one or more implementations of serializable classes, then you must put the #define line after the last IMPLEMENT_SERIAL macro call.

This function works only in the Debug version of MFC.


// example for AfxCheckMemory
CAge* pcage = new CAge( 21 );  // CAge is derived from CObject.
Age* page = new Age( 22 );     // Age is NOT derived from CObject.
*(((char*) pcage) - 1) = 99;   // Corrupt preceding guard byte
*(((char*) page) - 1) = 99;    // Corrupt preceding guard byte

The results from the program are as follows:

memory check error at $0067495F = $63, should be $FD
DAMAGE: before Non-Object block at $00674960
Non-Object allocated at file test02.cxx(48)
Non-Object located at $00674960 is 2 bytes long
memory check error at $00674905 = $63, should be $FD
DAMAGE: before Object block at $00674906
Object allocated at file test02.cxx(47)
Object located at $00674906 is 6 bytes long

See Also

MFC Macros and Globals