[gmx-developers] memory leak debugging ?

Erik Lindahl lindahl at stanford.edu
Fri Aug 9 19:31:34 CEST 2002


Hi,

> thanks a lot. Now if I write my own programs (partly for/with links/refs
> to gromacs libs, etc.), what do I have to do to enable them too for
> dmalloc debugging ? Do I have to link to a special object file ?

Just add the debugging malloc library to the link line - it will
replace the versions of malloc and free in the C library with 
error-checking ones.

> 
> Usually I use the Gromacs memory routines (snew, sfree, ...) .. so is
> this sufficient for having the debugging work ? (But my recent error
> was actually by closing a file twice .. so I don't know whether that
> would be debugged, too)

Well, it depends. snew() checks that we were really able to allocate the
memory, so it will catch out-of-memory problems, but not memory leaks,
freeing memory twice, writing past the end of strings or arrays, since
that would cause the code to run slower.

dmalloc (and most similar libraries) will find most memory leaks, and
commercial programs like insure will even detect when you are writing
past the end of strings.

One good recommendation (which we don't follow in Gromacs yet ;-) is to
never use the function strcpy() - replace it with strncpy() to make sure
you never write more bytes than the amount of memory you have.

Since Berk has been running insure we haven't really bothered to check
recent improvements of debugging memory libs on linux - they are 
probably better than when I last tested it...

Cheers,

Erik




More information about the gromacs.org_gmx-developers mailing list