I had a problem with finding memory leaks. Tell me, please, what tool is best to use for this? I have seen more than 50 debuggers in Wikipedia. Which to choose? (for windows)


Answer Summary:
0 Comments   [ + ] Show Comments


Please log in to comment

Community Chosen Answer


How to detect memory leak? 
The rough universal-valid method is to comment parts of your code starting from the initial object declarations and first function calls, and then continue narrowing that memory-leaking code commenting. In the end you'll get a small-enough piece of code where it should be easy enough to spot the problem.

Answered 04/03/2013 by: Bazylio
Yellow Belt

  • Things You'll Need

    Proficiency in C++
    C++ compiler
    Debugger and other investigative software tools
    Understand the operator basics. The C++ operator "new" allocates heap memory. The "delete" operator frees heap memory. For every "new," you should use a "delete" so that you free the same memory you allocated:

    char* str = new char [30]; // Allocate 30 bytes to house a string.

    delete [] str; // Clear those 30 bytes and make str point nowhere.
    Reallocate memory only if you've deleted. In the code below, str acquires a new address with the second allocation. The first address is lost irretrievably, and so are the 30 bytes that it pointed to. Now they're impossible to free, and you have a memory leak:

    char* str = new char [30]; // Give str a memory address.

    // delete [] str; // Remove the first comment marking in this line to correct.

    str = new char [60]; /* Give str another memory address with
    the first one gone forever.*/

    delete [] str; // This deletes the 60 bytes, not just the first 30.
    Watch those pointer assignments. Every dynamic variable (allocated memory on the heap) needs to be associated with a pointer. When a dynamic variable becomes disassociated from its pointer(s), it becomes impossible to erase. Again, this results in a memory leak:

    char* str1 = new char [30];

    char* str2 = new char [40];

    strcpy(str1, "Memory leak");

    str2 = str1; // Bad! Now the 40 bytes are impossible to free.

    delete [] str2; // This deletes the 30 bytes.

    delete [] str1; // Possible access violation. What a disaster!
    Be careful with local pointers. A pointer you declare in a function is allocated on the stack, but the dynamic variable it points to is allocated on the heap. If you don't delete it, it will persist after the program exits from the function:

    void Leak(int x){

    char* p = new char [x];

    // delete [] p; // Remove the first comment marking to correct.

    Pay attention to the square braces after "delete." Use "delete" by itself to free a single object. Use "delete" [] with square brackets to free a heap array. Don't do something like this:

    char* one = new char;

    delete [] one; // Wrong

    char* many = new char [30];

    delete many; // Wrong!
    If the leak yet allowed - I'm usually seeking it with deleaker.
    • Good choice. I use deleaker + cppcheck
  • Grete!!
    Most the code I've worked on (which is in C and at three different companies) uses a standard format to reduce the chances of memory leaks. The form involves:
    1. Free memory at the same level of the function heiarchy it was allocated in.
    a. If you have a create function, then have a delete.
    b. If CreateFoo, calls CreateBar, then have a DeleteFoo that calls DeleteBar.
    2. If an error happens in a function, free all memory allocated by that function before returning. [see example]
    a. If the erorr happens in CreateFoo, try to use DeleteFoo to do the cleanup. Or a helper function that is also used by DeleteFoo.
    b. Use separate variables for the local object creation from the pointers used to return an object.
    3. Make all the error handling in each function as similar to other functions as possible.
  • Wow! It is a complete guide!
  • Good advices, but they dont always work. Sometimes it is necessary to use a third-party debugging methods to detect errors.
Please log in to comment



We will need a few more details. Which programming\scripting language are you talking about? Is this a small script? If so we might be able to help if you post your code. If this is a fairly major programming project your question might be a little outside the scope of this forum.

Answered 03/20/2013 by: Ben M
Ninth Degree Black Belt

Please log in to comment

Sorry, I forgot to specify, I would like to know about C++ (visual studio).
Yesterday I found the information about memory management, but did not quite understand how it works 

Answered 03/21/2013 by: gromret
Orange Senior Belt

  • I'm afraid that's a little beyond my abilities to answer. Its been a long time since I took my c++ classes in college (well... 2 years, but... that's a long time for a teenager :). I'd recommend looking around in this forum: http://stackoverflow.com/. It's more oriented towards programming and you'd probably find more help there. I would definitely do some searching and reading before posting..
Please log in to comment

Yes, there are many tools for windows in Wikipedia. Therefore, I can specify the popular tools - vld, purify, deleaker, windbg, memcheck...

Answered 03/25/2013 by: Robert Miles
Senior White Belt

Please log in to comment

You can find more information on http://www.linkedin.com or http://stackoverflow.com/
There are so much information about memory leaks, it is a common problem 

Answered 03/27/2013 by: Robert Miles
Senior White Belt

  • Thanks for the link. I am not acquainted with this resource
Please log in to comment

I thought that this problem is rare. It turns out that even experienced programmers have memory leaks!!

Answered 04/01/2013 by: gromret
Orange Senior Belt

Please log in to comment

Thank you. I looked at this forum. But everyone advises using valgrind (this is for Linux). It is difficult to select a tool for windows

Answered 03/22/2013 by: gromret
Orange Senior Belt

Please log in to comment

Thank you all for your help. Topic closed

Answered 04/02/2013 by: gromret
Orange Senior Belt

Please log in to comment

Thank you very much for your answers and advices

Answered 04/19/2013 by: gromret
Orange Senior Belt

Please log in to comment

You reassured me)) I made sure that memory leaks often occur in the code ((

Answered 04/04/2013 by: gromret
Orange Senior Belt

Please log in to comment

Thank you. I should know more details about these tools before I start using them.

Answered 03/26/2013 by: gromret
Orange Senior Belt

Please log in to comment
Answer this question or Comment on this question for clarity