Looking for Memory Leak Advice

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • xarzu
    New Member
    • Apr 2007
    • 90

    Looking for Memory Leak Advice

    Apart from buying BoundsChecker or any of the other tools to track down memory leaks. Does anyone have any advice or tips from your years of programming experience to offer on how to track down the causes of memory leaks?

    I find that the big dump that happens at the end of a program does not typically lend any useful clues as to where or what was allocated that was not freed. I find that I have to back track and comment out sections of code until through the process of elimination and trial-and-error, I am able to narrow down where the problem is. And since this is usually at the bottom of the To-Do list, the memory leaks are not plugged.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by xarzu
    I find that the big dump that happens at the end of a program does not typically lend any useful clues as to where or what was allocated that was not freed.
    What platform? If you are talking about MS Visual C++ then find this dump very useful because with a little luck if you run the program in the debugger again without any recompilation then because of the virtual address space all the memory blocks get allocated at the same addresses unless there is some marginal timing issue at play as well.

    In this case you can set a break point in malloc (or new) that halts when it allocates the block with an address that is leaking and then from the call stack you can tell where it was in your code that allocated this block and that should provide some clues as to what the program is failing to deallocate.

    If that doesn't work you can do things like route all allocations through a function of your own (#define malloc(x) MyMalloc((x), __FILE__, __LINE__) works wonders) and in that function increase the block size to save some extra data about where the block was allocated from at the start of the block. Then your dump will print this data giving you information about where the block was allocated.

    Comment

    • ashitpro
      Recognized Expert Contributor
      • Aug 2007
      • 542

      #3
      If you are talking about Linux platform, then you are lucky.
      use "valgrind" to detect memory leaks.
      It's free and 100% reliable and easy to use.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by xarzu
        Apart from buying BoundsChecker or any of the other tools to track down memory leaks. Does anyone have any advice or tips from your years of programming experience to offer on how to track down the causes of memory leaks?
        Just get BoundsChecker. No amount of sniffing the code will remove all the leaks.

        My years of experience lead me in the direction for not having leaks in the first place. That means encapsulated new/delete that is under the control of C++ and not you or the compiler. Typically, this involves a lot of handles.

        And still I use BoundsChecker at the end. I trust no one.

        Comment

        Working...