How can we detect memory leaks in our program

3 11 2009

Description

 

 

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.

HowCanIDoIt1

 

 

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.

MyCodeSnippet

 

 

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"));
   }
}

MyNote

 

 

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

Advertisements

Actions

Information

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: