Rhyous

October 13, 2009

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

Filed under: Visual Studio — J. Abram barneck @ 5:36 am

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.

Advertisements

9 Comments »

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

    Comment by rhyous — October 27, 2009 @ 3:52 pm | Reply

  2. // 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

    Comment by Philipp H. Toner — May 21, 2010 @ 10:14 am | Reply

    • I will investigate.

      It would help if you mentioned why you think it is wrong.

      Comment by rhyous — May 21, 2010 @ 11:44 am | Reply

    • Ok…So I looked at it and I disagree that it is wrong. It works. Why do you think it is wrong?

      Comment by rhyous — May 21, 2010 @ 4:08 pm | Reply

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

    Thanks!

    Comment by Piotr — August 9, 2010 @ 1:52 pm | Reply

  4. Big thx =)

    Comment by hunterra — March 2, 2011 @ 1:00 am | Reply

  5. 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.

    Comment by YG — October 25, 2011 @ 1:33 am | Reply

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

    Comment by Ronumous — November 7, 2011 @ 4:32 am | Reply

  7. Use valgrind for linux or deleaker for windows – it’s best debuggers!!!

    Comment by JohnDepth — February 16, 2012 @ 2:18 pm | Reply


RSS feed for comments on this post. TrackBack URI

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

Create a free website or blog at WordPress.com.

%d bloggers like this: