Sunday, September 7, 2008

CMemoryState - Detects memory leaks in your program

Memory leak occurs when memory for an object is allocated on the heap but not deallocated when it is no longer required. Such memory leaks can lead to out-of-memory errors.CMemoryState provides an excellent way to detect memory leaks in your program.

The CMemoryState diagnostics only help to detect memory leaks caused when memory allocated using the new operator is not deallocated using delete.

CMemoryState
will not address the leaks caused by malloc , LocalAlloc & GlobalAlloc.

How to Use: 

CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
/* Obtains a snapshot or "checkpoint" of the current memory state.*/

/* memory allocation activities go on here */

newMemState.Checkpoint();
if(diffMemState.Difference(oldMemState,newMemState))
{

     TRACE( "Memory leak Detected!\n" );
              diffMemState.DumpAllObjectsSince();

}
/* Difference() - Computes the difference between two objects of type CMemoryState.*/
/* DumpAllObjectsSince() - Dumps a summary of all currently allocated objects since a previous checkpoint.*/

cheers!

No comments: