Wednesday, 15 August 2012

linux - Detect C memory 'leaks' that are freed on exit -



linux - Detect C memory 'leaks' that are freed on exit -

assume have c programme (running under linux) manipulates many info structures, complex, several of can grow , shrink should not in general grow on time. programme observed have gradually increasing rss on time (more can explained memory fragmentation). want find leaking. running under valgrind obvious suggestion here, valgrind (with --leak-check=full , --show-reachables=yes) shows no leak. believe because info structures correctly beingness freed on exit, 1 of them growing during life of program. instance, there might linked list growing linearly on time, forgetting remove resource on list, exit cleanup correctly freeing items on list @ exit. there philosophical question whether these in fact 'leaks' if freed, of course of study (hence quote marks in question).

are there useful tools instrument this? i'd love ability run under valgrind , have produce study of current allocations on exit, have happen on signal , allow programme continue. stack trace signatures had growing allocations against them.

i can reliably nice big 'core' file gdb generate-core-file; there way analyse off-line, if compiled handy malloc() debugging library instrumented malloc()?

i have total access source, , can modify it, don't want instrument every info construction manually, , i'm interested in general solution problem (like valgrind provides) rather how address particular issue.

i've looked similar questions on here appear be:

why programme leak memory? how observe memory leaks @ exit? (no utilize me) how observe memory leaks core file? (great, none has satisfactory answer)

if running under solaris i'm guessing reply 'use handy dtrace script'.

valgrind includes gdbserver. means can utilize gdb connect it, , e.g. issue leak dump, or show reachable memory while running. ofcourse, have justice whether there "memory leak" or not, valgrind can't know if there's bug in application logic fails release memory, still maintain references it.

run valgrind --vgdb=yes flag , run commands:

valgrind --vgdb=yes --leak-check=full --show-reachable=yes ./yourprogram gdb ./yourprogram (gdb) target remote | vgdb (gdb) monitor leak_check total reachable

see docs more info, here , here

you can programatically in program

#include <valgrind/memcheck.h>

and @ appropriate place in code do:

valgrind_do_leak_check;

(iirc that'll show reachable memory too, long valgrind run --show-reachable=yes

c linux memory-leaks gdb valgrind

No comments:

Post a Comment