How to check SDI app for memory leaks ?

How to check SDI app for memory leaks ?

Hi, experts!
I used to check my Dialog-based programs for memory leaks with simple way :

BOOL CMyApp::InitInstance()
{       // simplified version of InitInstance(), some things were cut

      CMemoryState st1, st2, diff;

      CMyDlg dlg;
      m_pMainWnd = &dlg;
      int nResponse = dlg.DoModal();

            TRACE("\n\n\n\n\nMemory OK\n\n\n\n\n");
            TRACE("\n\n\n\n\nMemory LEAK\n\n\n\n\n");

      return FALSE;

Question :

Is it possible to check for memory leaks SDI app.
If YES, where should I place CheckPoint()'s and Difference() calls ?
I want to see "Memory OK" for SDI-app created by wizard.

Who is Participating?
chensuConnect With a Mentor Commented:
INFO: Foundation Class Debug Library Detects Memory Leaks
Vinayak KumbarSr Program ManagerCommented:

Run the SDI app in the debug mode. I mean say F5 to the app to start debugging. Then do all the operations in Ur app. Then exit the app. The Vc++ will dump U the memory leaks if detected in the output window(it does for dialog based app also:-)). From that U can know the memory leaks. Else if U want it to be done using CheckPoint, then declare the class objects in the app.h file as
CMemoryState st1, st2, diff;

Then, in the initinstance take one snap shot. Then map the ExitInstance() for Ur app, there take one mpre snap shot of memory and find the difference.

There are many tools available to check for memory leaks also.

Try it out.
 Just furthering VinExperts's comment regarding the tools.
The most commonly used tools are
1. Bounds Checker
2. MemCheck.

Bounds checker is really very good and applies for all WIN32 applications.
Else you will have to do what VinExpert says.

Unfortunately, BoundsChecker is well-known for singalling lots of false-positives.  Otherwise, it's a very valuable tool.

In the meantime, you can make your CMemoryState instances members of your CWinApp-derived class. Move your calls to Checkpoint() and Difference() to an override of ExitInstance().

This code (as well as the code you've presented) won't find all memory leaks, though--in particular, leaks caused by static objects won't be found.

..B ekiM
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.