Finding memory leaks
void MyFunction(void)
{
char * MyCharArray = new char[10];
.
.
.
//delete [ ]MyCharArray;
}
but we don't free it, running the program will produce the message:
Detected memory leaks!
Dumping objects ->
C:\vcpp32\myproject\test.cpp(62) : {39} normal block at 0x00780DF0, 10 bytes
long.
Data: < > CD CD CD CD CD CD CD CD CD CD
Object dump complete.
Now in this case, it gave us the CPP file and line number, but often it does not. One way to find the leak would be to put a breakpoint in memory at the memory address used by the allocation (assuming it's constant).
But there's a better way!
The number in the curly brackets, is an allocation number. Each allocation receives a serial number. When you get a leak, you can use this number to automatically place a breakpoint at the point in the program where the allocation is being made. To do this, you can do one of two things.
1. Add a call to the function _CrtSetBreakAlloc(), giving it the number in the curly brackets.
2. In the debugger, set the value of the variable _crtBreakAlloc to that number.
You want to do this as early in the program as possible. The effect of doing that is a breakpoint on the line of the allocation of the 'new' function. You then use the call stack to find the exact place in the program that called the allocation. (See MSDN article Q151585 for more information)

Comments
mem shared between apps?
Posted by Legacy on 02/13/2004 12:00amOriginally posted by: Phil Beck
I have three applications running on an NT4 machine. They share several dlls that were written in house.
If I run any of them under the MSVC++ 5 debugger, then I get no reported memory leaks. However, all three applications appear to slowly increase in the amount of memory they use. The only thing that tells me this is the memory column in the processes page of task manager.
If I then shut one of the apps down, the other two apps suddenly drop down to their original memory usage.
I am quite experienced in using the C Runtime debug heap and _CrtSetAllocHook().
I'm not too sure where the MFC version in MSVC++5 stands on its abililty to dump memory leaks.
Thanks
ReplyPhil.
"_crtBreakAlloc" undefined
Posted by Legacy on 03/04/2003 12:00amOriginally posted by: pat
Hello,
I tried setting _CrtSetBreakAlloc() giving it the number in the curly brackets I get in the debug window.
I run my app in debug-mode and wanted to access "_crtBreakAlloc" in the Watch-Window, but it was undefined ((CXXX0017: Error:: symbol "_crtBreakAlloc" not found))!
What's my mistake?
Thanks for your help
Pat
-
Reply"_crtBreakAlloc" undefined reply
Posted by happy_red on 07/21/2004 04:40amIf you are linking against the multithreaded runtime dll. Try puting "{,,msvcrtd.dll}_crtBreakAlloc" in your watch window.
ReplyFind who and not were is the memory leak!!!
Posted by Legacy on 12/16/2002 12:00amOriginally posted by: PaoloB
This tecnique is only valid for memory leak generated from class with vtable.
So, if you have this:
normal block at 0x048B80C0, 16 bytes long.
Data: < $ > 18 DA 24 01 00 00 00 00 00 00 00 00 00 00 00 00
insert in your application a code for ask
a string and
CString cl = "0124DA18"; // usualy ask like inputbox :-)
char * foo = " ";
char * p;
void * ptr = (void *)strtoul(cl, &p, 16);
debug ptr to see vtable description:
ptr 0x0124da18 const ClassUnDeleted::`vftable'
byr
ReplyHow to detect memory leaks !!!
Posted by Legacy on 11/07/2002 12:00amOriginally posted by: Narayanan N
Friends,
Has anyone used the umdhtools from Microsoft for dectecting the memory leaks in Windows XP??
ReplyNarayanan
See Knowledge Base article Q151585
Posted by Legacy on 07/03/2002 12:00amOriginally posted by: Jonathan
Reply
Can any one help me to find memory leak
Posted by Legacy on 06/27/2002 12:00amOriginally posted by: shashi
ReplyMemory Leaks in Normal blocks
Posted by Legacy on 04/05/2002 12:00amOriginally posted by: Farooq Mulla
I am stuck with a memory leak where memory leaks occur in steps of 4kb which makes my mem size above 7MB ....
I checked out the dumping objects ---> created after debugging to get the memory leaks which showed up a couple of pointers i hadnt released which i rectified ,but even then it shows memory leak in plex.cpp (which is a part of the VC++) the message reads something like this
plex.cpp (line 31) <memory location> 164 bytes in Normal blocks
(this line appears for large no of times,at different mem locations which amounts to quite a big mem leak 164bytes*n)
wheras the free blocks occupies 0 bytes & dbg blocks occupies 100 bytes
how do i remove this memory attached in normal blocks ..
whats this concept of normal blocks,dbg blocks & free blocks...
could u plz give me a hint to this.
ReplySkip Memory Leaks of Global variables ...
Posted by Legacy on 03/21/2002 12:00amOriginally posted by: jesvh
ReplyNeed help in writing memory dump to a file
Posted by Legacy on 11/22/2001 12:00amOriginally posted by: Nagaraj Rao
In VC++ under debug windows, I can use memory window to see contents of particular memory location. Is there a way to write this data( which is suppose to be in HEX) into a file by specifying address ranges ? Does Visual studio has some options ?
Ex: If I want to write data contents from memory location to 0x1000 to 0x2000, how do do it ?
regards
Nagaraj Rao
ReplyTesting tools for memory leaks in complex applications
Posted by Legacy on 08/05/1999 12:00amOriginally posted by: Abdul Hafeez
Hi friends,
The techniques for tracking memory leaks are excellent, but in a practical software development we can't be sure if all the control paths of execution are free of memory leaks.
I have a suggesion regarding testing of memory leaks. There are software likes BoundsChecker , Verify etc., which are efficient in testing the memory leaks. Once you install softwares they will get integrated with the Visual Studio environment and are easy to test the applications.
Here in we can get the exact details with analysis of all the memory leaks.
Thanks,
ReplyAbdul Hafeez
Loading, Please Wait ...