Finding memory leaks

I recently came across something that I found to be a major improvement in my ability to program in MS Developer Studio version 5. It concerns memory leaks. When running the Debugger, if you have the Dump function on, you get a list of objects that did not get deallocated. These are defined as 'Memory Leaks'. If we allocate memory for a character array in a function:
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:00am

    Originally 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
    Phil.

    Reply
  • "_crtBreakAlloc" undefined

    Posted by Legacy on 03/04/2003 12:00am

    Originally 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

    • "_crtBreakAlloc" undefined reply

      Posted by happy_red on 07/21/2004 04:40am

      If you are linking against the multithreaded runtime dll. Try puting "{,,msvcrtd.dll}_crtBreakAlloc" in your watch window.

      Reply
    Reply
  • Find who and not were is the memory leak!!!

    Posted by Legacy on 12/16/2002 12:00am

    Originally 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

    Reply
  • How to detect memory leaks !!!

    Posted by Legacy on 11/07/2002 12:00am

    Originally posted by: Narayanan N

    Friends,
    Has anyone used the umdhtools from Microsoft for dectecting the memory leaks in Windows XP??


    Narayanan

    Reply
  • See Knowledge Base article Q151585

    Posted by Legacy on 07/03/2002 12:00am

    Originally posted by: Jonathan

    Reply
  • Can any one help me to find memory leak

    Posted by Legacy on 06/27/2002 12:00am

    Originally posted by: shashi

    How to detect memory leaks if some thing like below appears in your debug window after completion of debugging.
    
    Detected memory leaks!
    Dumping objects ->
    {318} normal block at 0x02077700, 60 bytes long.
    Data: <8 > 38 87 0F 10 CD CD CD CD 00 CD CD CD CD CD CD CD
    Object dump complete.
    The thread 0x124 has exited with code 0 (0x0).
    The program 'D:\Tester\Debug\Tester.exe' has exited with code 0 (0x0).


    Shashi

    Reply
  • Memory Leaks in Normal blocks

    Posted by Legacy on 04/05/2002 12:00am

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

    Reply
  • Skip Memory Leaks of Global variables ...

    Posted by Legacy on 03/21/2002 12:00am

    Originally posted by: jesvh

    Hello, all:
    
    

    If a c run time program (win32 console application) with
    several const global c++ class instances (not use MFC) .

    How do I skip memory-leak messages of them ?
    _CrtDumpMemoryLeaks only works before program exit. but
    global class instance destructors work after that .....

    example:

    class AA
    {
    char *pp;
    .....

    ~AA() { free(pp); };
    }


    const AA a-obj;


    void main()
    {

    .....
    .....
    .....

    _CrtDumpMemoryLeaks();
    }


    pp will be showed as memory leak .... but it doesn't.
    Is there any method to skip it ?

    Reply
  • Need help in writing memory dump to a file

    Posted by Legacy on 11/22/2001 12:00am

    Originally 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

    Reply
  • Testing tools for memory leaks in complex applications

    Posted by Legacy on 08/05/1999 12:00am

    Originally 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,
    Abdul Hafeez

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Protecting business operations means shifting the priorities around availability from disaster recovery to business continuity. Enterprises are shifting their focus from recovery from a disaster to preventing the disaster in the first place. With this change in mindset, disaster recovery is no longer the first line of defense; the organizations with a smarter business continuity practice are less impacted when disasters strike. This SmartSelect will provide insight to help guide your enterprise toward better …

  • Intelligent N+X Redundancy, Placement Affinities, & Future Proofing in the Virtualized Data Center Virtualization brought about the ability to simplify business continuity management in IT. Workload portability and data replication capabilities mean that physical infrastructure failures no longer need impact application services, and they can rapidly be recovered even in the event of complete site failure. However, Enterprises and Service Providers face new challenges ensuring they have enough compute …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds