在Linux环境下查找C代码中的内存泄漏

发布于 2024-09-16 01:22:45 字数 172 浏览 7 评论 0原文

你好,我在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦归所梦 2024-09-23 01:22:45

它无法告诉您指针的名称,因为内存泄漏的整个概念是没有指针再指向内存(至少,对于 Valgrind 描述的泄漏类型) “肯定输了”)。

可以告诉您的是分配内存的源文件和行号 - 然后您需要在源文件中查找该行以找出内存应该在哪里 em> 被释放。例如,如果 Valgrind 损失记录如下所示:

==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)

那么您需要查看 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:

==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.

樱&纷飞 2024-09-23 01:22:45

你没说你用的是哪个编译器,我想是 gcc 吧?
您是否使用 -g 来包含调试符号?

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文