在Linux环境下查找C代码中的内存泄漏
你好,我在使用 valgrind 时遇到问题 当我将它与 valgrind --leak-check=full 一起使用时,然后执行文件的名称它会告诉我内存泄漏在哪些块中,但是当我找不到我使用 free 的指针时。 是否有某种标志可以告诉指针的名称。 如果有任何人告诉我 Visual Studio 上的泄漏位置,我也非常想听听
hello i got a problem using valgrind
when i use it with valgrind --leak-check=full
and afterwards 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它无法告诉您指针的名称,因为内存泄漏的整个概念是没有指针再指向内存(至少,对于 Valgrind 描述的泄漏类型) “肯定输了”)。
它可以告诉您的是分配内存的源文件和行号 - 然后您需要在源文件中查找该行以找出内存应该在哪里 em> 被释放。例如,如果 Valgrind 损失记录如下所示:
那么您需要查看
foo.c
中的第 1161 行,该行位于函数do_foo()
内。这就是内存分配的地方(使用malloc()
),只有你可以说出应该在哪里释放它。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:
Then you need to look at line 1161 in
foo.c
, which is within the functiondo_foo()
. That's where the memory was allocated (withmalloc()
), and only you can say where it should have been freed.你没说你用的是哪个编译器,我想是 gcc 吧?
您是否使用
-g
来包含调试符号?You didn't say which compiler you are using, I suppose gcc?
Do you use
-g
to have debugging symbols included?