How to find the file and line number of memory leaks using C++ with wxWidgets in Visual Studio 2008?

Ok, so I am coding with C++ and wxWidgets using Visual Studio 2008 as the IDE.

I got the following output when my my program was launched in debug mode and it exited.

Detected memory leaks!
Dumping objects ->
{1535} normal block at 0x005C1920, 18 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CD CD

I can’t have memory leaks and while they aren’t a big deal and are with objects I create once so they really aren’t that bad, my obsessive compulsiveness (I’m just a little OC but not OCD) wouldn’t let me move on with the program or do anything else until I had solved these memory leaks.

I did some researching and tried a lot of things before I finally found this website:
http://www.litwindow.com/Knowhow/wxHowto/wxhowto.html#debug_alloc

So I gave the steps a try. I had a little bit of a problem but I got them to work, so I am re-writing the steps so that I remember how to do it again and don’t run into the same problem.

Steps for Finding Memory Leaks in C++ and wxWidgets using Visual Studio 2008

  1. Create a new header (.h) file called: stdwx.h 
    // wxWidgets precompiled / standard headers
    #include "wx/wxprec.h"
    
    // When debugging changes all calls to "new" to be calls to "DEBUG_NEW" allowing for memory leaks to
    // give you the file name and line number where it occurred.
    #ifdef _DEBUG
    	#include <crtdbg.h>
    	#define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__)
    	#define new DEBUG_NEW
    #else
    	#define DEBUG_NEW new
    #endif
    

    Note: The site I linked to had much more in the header file, but I like to know the minimal requirements for the task at hand and so I commented out the lines that I thought didn’t matter and tested by recompiling and running in debug and sure enough, only the above lines are needed. However, that shouldn’t stop you from adding #includes you always use to your header file. Notice the use of #ifdef _DEBUG which means that this code will only be used when debugging and so your release code will not contain this debugging code (which is useless for release builds).

  2. Create a new .cpp file called: stdwx.cpp. Add A single line including stdwx.h.Yes, it is really only supposed to be one #include line as shown:
    #include "stdwx.h"
    
  3. Now add that same #include line to every .cpp file in your project:
    // Include the stdwx.h in every .cpp file
    #include "stdwx.h"
    
  4. Now in Visual Studio 2008, under Solution Explorer, right-click on the Project (my test project happens to be Dice at the moment) and choose Properties.
  5. Expand Configuration Properties | C/C++ and select Precompiled headers.
  6. Use the drop down for Create/Use Precompiled Header to select Create Precompiled Header (/Yc).
  7. Under Create/Use PCH Through File, type in stdwx.h.Note: The Precompiled Header File setting should auto-popluate with the correct value of $(IntDir)\$(TargetName).pch.
  8. Click OK to save the project properties.

A screen shot is included to provide further help on these settings:

Precompiled Headers Settings

Precompiled Headers Settings

You should now be ready to recompile your program and now instead of seeing just vague memory locations of memory leaks, you should see the exact file and line number, which is key in detecting and deleting the allocated memory.

Detected memory leaks!
Dumping objects ->
{1535} normal block at 0x005C1920, 18 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 CD CD
c:\users\jbarneck\documents\visual studio 2008\projects\dice\dice\die.cpp(183) : {1529} normal block at 0x005C18D0, 20 bytes long.
 Data: <          \ . \ > 00 00 00 00 CD CD CD CD 20 19 5C 00 2E 19 5C 00

Copyright ® Rhyous.com – Linking to this article is allowed without permission and as many as ten lines of this article can be used along with this link. Any other use of this article is allowed only by permission of Rhyous.com.

9 Comments

  1. JohnDepth says:

    Use valgrind for linux or deleaker for windows - it's best debuggers!!!

  2. Ronumous says:

    I just use visual leak detector, you only need to include it in the top level file...

  3. YG says:

    Just checked every step in the article, still can't figure out why it doesn't work for me.

    ps. The VS2008 ide would give me lots of c2603/c2064/c3801 if i put the #include "stdwx.h" behind other includes.

  4. Piotr says:

    It works for me (VS2008, boost, wxWidgets), just needs to be included after all #include commands in each *.cpp

    Thanks!

  5. Philipp H. Toner says:

    // wxWidgets precompiled / standard headers
    #include "wx/wxprec.h"

    // When debugging changes all calls to "new" to be calls to "DEBUG_NEW" allowing for memory leaks to
    // give you the file name and line number where it occurred.
    #ifdef _DEBUG
    #include
    #define _CRTDBG_MAP_ALLOC
    #define DEBUG_NEW new(_NORMAL_BLOCK ,__FILE__, __LINE__)
    //#define new DEBUG_NEW !!!!!! THIS IS WRONG!
    #else
    #define DEBUG_NEW new
    #endif

  6. rhyous says:

    Sorry I had the wrong image there for a while, I fixed it.

Leave a Reply

How to post code in comments?