Dumping All Objects

This topic explains how to obtain a diagnostic dump of all objects in your program.

For information on dumping C run-time objects, see Using the Debug Heap and _CrtSetDbgFlag.

DumpAllObjectsSince dumps out a description of all objects detected on the heap that have not been deallocated. As the name implies, DumpAllObjectsSince dumps all objects allocated since the last Checkpoint. However, if no Checkpoint has taken place, all objects and nonobjects currently in memory are dumped.

Note   Before you can use MFC object dumping, you must enable diagnostic tracing. See the Note in the topic Diagnostics.

To dump all objects

  • Expanding on the example shown in Detecting a Memory Leak in the topic Detecting Memory Leaks, the following code dumps all objects that have not been deallocated when a memory leak is detected:

    if( diffMemState.Difference( oldMemState, newMemState ) )
    {
        TRACE( "Memory leaked!\n" );
        diffMemState.DumpAllObjectsSince();
    }
    

    A sample dump from the preceding example is shown here:

    Dumping objects ->
    
    {5} strcore.cpp(80) : non-object block at $00A7521A, 9 bytes long
    {4} strcore.cpp(80) : non-object block at $00A751F8, 5 bytes long
    {3} strcore.cpp(80) : non-object block at $00A751D6, 6 bytes long
    {2} a CPerson at $51A4
    
    Last Name: Smith
    First Name: Alan
    Phone #: 581-0215
    
    {1} strcore.cpp(80) : non-object block at $00A7516E, 25 bytes long
    

    The numbers in braces at the beginning of most lines specify the order in which the objects were allocated. The most recently allocated object appears first. You can use these ordering numbers to help identify allocated objects.

To set a breakpoint when a particular allocation occurs, first start your application in the debugger. Set the global variable _afxBreakAlloc to the number in braces discussed earlier. This will set a conditional breakpoint in your application that will trigger when the allocation you specify is being made. Looking at the call stack at this point will tell you the path your program took to get to the specified allocation.

The C run-time library has a similar function, _CrtSetBreakAlloc, that you can use for C run-time allocations.