Re: command line tool for finding mem leaks?



In article <Pine.OSX.4.64.0605102222360.1466@xxxxxxxxx>,
ilya <ilyamoskovko@xxxxxxxxx> wrote:

can anybody recommend me a tool that will automatically detect
memory leaks in my c++ code compiled with g++. i'm looking for
something similar to what bcheck or dbx does [when you do
'check -leaks']. if it can identify actual lines in code where
the leak(s) occur that would be even better.

i had no luck with gdb; i know there is the leaks command but
i'm not exactly sure how to use it on my program. i have tried
some other tools, including MallocDebug, but couldn't get any
of them to work on my intel based mac.

Did you type into a Terminal window:

man leaks

?

As the man page says, you'll want to set the environment variable
MallocStackLogging = 1, so leaks will give you a human readable dump of
the top few stack frames of the allocation of each leak.

I usually do a command in the program under test, then in a Terminal
window, while my program is still running, but idle, do:

ps ax -- to find the process id number {pid}
leaks {pid} > take1.txt

then do the command a second time, and do:

leaks {pid} > take2.txt



then use FileMerge to see what is different between the two files. Any
allocations made by the second pass and not cleaned up will jump out at
you.

Once you know who allocate the blocks that leak, you can look for the
highest level structure that owns some leaking blocks, then look for
what should have deallocated it. Very common:

foo = fum;

which should be:

if(foo != fum){
delete foo;
foo = fum;
}

Note: if you are using Cocoa, you must turn off NSZombie, since cocoa's
Zombie detection requires "leaking" every block ever allocated as far as
your program is concerned (blocks you free are kept on a list so that
accesses after death can be tracked.)
.



Relevant Pages

  • Re: GC.Collect can be trusted?
    ... on the allocation pattern. ... small managed memory leaks by using a profiler alone. ... I can tell you now that I have very little time to spend tracking down bugs in the GC. ... What I'm after is being able to track down memory leaks that I unwittingly create in the code I write. ...
    (microsoft.public.dotnet.languages.csharp)
  • A lowly newbie humbly asks for a clue
    ... A memory block has been leaked. ... The allocation number is: 1301 ... Current memory dump of 256 bytes starting at pointer address 2444AD8: ... The small block leaks are (excluding ...
    (borland.public.delphi.language.basm)
  • Re: CRT block leaks
    ... I want to get rid of leaks in the ... my application does not break at the allocation number. ... Validator and set the DLLs to Hook feature to identify your DLL only, ...
    (microsoft.public.vc.debugger)
  • Re: Fixing Memory leak in ATL
    ... > My atl activex container leaks memory one time every time I run the ... > Object dump complete. ... They are called allocation order numbers. ...
    (microsoft.public.vc.atl)