Detecting Memory Leaks VS2008

17 views
Skip to first unread message

Mark Erbaugh

unread,
Dec 14, 2009, 1:32:12 PM12/14/09
to wx-u...@googlegroups.com
I'm using MS VS2008 Express edition on Windows XP. I originally compiled wxWidgets 2.8.10 with the recommended 0 for wxUSE_DEBUG_CONTEXT, wxUSE_MEMORY_TRACING, wxGLOBAL_MEMORY_OPERATORS and wsUSE_DEBUG_NEW_ALWAYS. When I had memory allocation problems in my program, VS would report memory leaks although, the memory dump it gave wasn't very helpful in determining where the problem was.

I read the section on Detecting Memory Leaks and Other Errors in "Cross Platform GUI Programming with wxWidgets" (page 416 in my copy). It mentioned setting the above constants to 1. I rebuilt the debug version of wxWidgets with those settings and ran a program with deliberate memory leaks (I removed the code that deleted some objects that were created with new) in VS. It reported the memory leaks, but didn't give the details of the type of object and program location as shown in the book.

It reported everything as a non-object, even though I had DECLARE_CLASS / IMPLEMENT_CLASS macros for most (or all) of the wxObject descendents in my program:

==== with deliberate memory leak ====

There were memory leaks.


----- Memory dump -----

non-object data at 0x1050628, size 48

non-object data at 0x10506C0, size 12

< many more similar lines>

----- Memory statistics -----

700 objects of class nonobject, total size 14799



Number of object items: 0

Number of non-object items: 700

Total allocated size: 14799

=========================

I removed the deliberate memory leak from my code. I'm now still getting a report of memory leaks:

==== without deliberate memory leak ====

There were memory leaks.


----- Memory dump -----





----- Memory statistics -----

126 objects of class nonobject, total size 3760



Number of object items: 0

Number of non-object items: 126

Total allocated size: 3760

==============



VS does not report any memory leaks. However, my program has some strange behavior, it has some problems running in stand-alone release mode that aren't present when running in either debug or release mode inside the VS environment, so I suspect that there may be some memory corruption.

Thanks,
Mark

BigPilot

unread,
Dec 15, 2009, 8:57:28 AM12/15/09
to wx-users

On Dec 14, 7:32 pm, Mark Erbaugh <m...@microenh.com> wrote:
> I'm using MS VS2008 Express edition on Windows XP. I originally compiled wxWidgets 2.8.10 with the recommended 0 for wxUSE_DEBUG_CONTEXT, wxUSE_MEMORY_TRACING, wxGLOBAL_MEMORY_OPERATORS and wsUSE_DEBUG_NEW_ALWAYS. When I had memory allocation problems in my program, VS would report memory leaks although, the memory dump it gave wasn't very helpful in determining where the problem was.
>
> I read the section on Detecting Memory Leaks and Other Errors in "Cross Platform GUI Programming with wxWidgets" (page 416 in my copy). >It mentioned setting the above constants to 1. I rebuilt the debug version of wxWidgets with those settings and ran a program with >deliberate memory leaks (I removed the code that deleted some objects that were created with new) in VS. It reported the memory leaks, >but didn't give the details of the type of object and program location as shown in the book.

I have used Visual Leak Detector, a free open-source leak detector,
for years with wxWidgets and Visual C++ and it helped me squash a lot
of hard-to-find memory leaks. Try it.

http://www.codeproject.com/KB/applications/visualleakdetector.aspx


Mark Erbaugh

unread,
Dec 15, 2009, 9:13:35 AM12/15/09
to wx-u...@googlegroups.com

On Dec 15, 2009, at 8:57 AM, BigPilot wrote:

I have used Visual Leak Detector, a free open-source leak detector,
for years with wxWidgets and Visual C++ and it helped me squash a lot
of hard-to-find memory leaks. Try it.

http://www.codeproject.com/KB/applications/visualleakdetector.aspx


Thanks,
Mark

Kenneth Porter

unread,
Dec 16, 2009, 6:31:22 AM12/16/09
to wx-u...@googlegroups.com
--On Tuesday, December 15, 2009 5:57 AM -0800 BigPilot
<bigp...@linuxmail.org> wrote:

> I have used Visual Leak Detector, a free open-source leak detector,
> for years with wxWidgets and Visual C++ and it helped me squash a lot
> of hard-to-find memory leaks. Try it.

Agreed, that's a good one.

For harder to find ones, look at the gflags utility that's part of
Microsoft's free-to-download debugging tools package. It can change the
kernel allocation flags so that you get extra guard pages and fast free
operations, so that any touching of a page after it's freed is immediately
caught by the hardware.

<http://www.microsoft.com/whdc/DevTools/Debugging/default.mspx>
Reply all
Reply to author
Forward
0 new messages