开发者

finding memory leaks in c code on linux environment

hello i got a problem using valgrind when i use it with valgrind --leak-check=full and afterwards开发者_如何学JAVA the name of excution file it tells me in which blocks the memory leak is but when i cant find to which pointer i did use free. is there some sort of flag that tells the name of the pointer. if there is anyway to tell me where the leak is on visual studio i would very much like to hear about it too


It can't tell you the name of the pointer, because the whole idea of a memory leak is that no pointer points at the memory any more (at least, for the kinds of leaks that Valgrind describes as "definitely lost").

What it can tell you is the source file and line number where the memory was allocated - you then will need to look up that line in your source to figure out where the memory is supposed to be deallocated. For example, if the Valgrind loss record looks like:

==17110== 49 bytes in 1 blocks are definitely lost in loss record 17 of 35
==17110==    at 0x4023D6E: malloc (vg_replace_malloc.c:207)
==17110==    by 0x80C4CF8: do_foo (foo.c:1161)
==17110==    by 0x80AE325: xyzzy (bar.c:466)
==17110==    by 0x8097C46: io (bar.c:950)
==17110==    by 0x8098163: main (quux.c:1291)

Then you need to look at line 1161 in foo.c, which is within the function do_foo(). That's where the memory was allocated (with malloc()), and only you can say where it should have been freed.


You didn't say which compiler you are using, I suppose gcc? Do you use -g to have debugging symbols included?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜