/*
We know that memory is always at a premium. So as a programmer we have to to ensure minimum memory consumption as possible and necessary to avoid memory leaks from the program. How can we detect memory leaks in our program.
MFC provides a structure CMemoryState to detect memory leaks in our program. This diagnostics method 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.
*/
void DetectMemoryLeak()
{
CMemoryState oldMemState, newMemState, diffMemState;
oldMemState.Checkpoint();
/* new memory is allocated in the heap */
int *p = new int;
/* heap memory is not yet deallocated */
newMemState.Checkpoint();
if(diffMemState.Difference(oldMemState,newMemState))
{
AfxMessageBox(_T("Detects Memory Leak"));
}
}