如何让 Valgrind 显示行错误?

发布于 2024-12-10 13:04:12 字数 897 浏览 1 评论 0原文

如何让 Valgrind 准确显示错误发生的位置?我编译了我的程序(通过 PuTTy 在 Linux 终端上的 Windows 机器上),添加了 -g 调试选项。

当我运行 Valgrind 时,我得到泄漏和堆摘要,并且我肯定丢失了内存,但我从未获得有关它发生的位置的信息(文件名、行)。 Valgrind 不应该在我分配内存后在哪一行告诉我,稍后它无法释放内存吗?

==15746==
==15746== HEAP SUMMARY:
==15746==     in use at exit: 54 bytes in 6 blocks
==15746==   total heap usage: 295 allocs, 289 frees, 11,029 bytes allocated
==15746==
==15746== LEAK SUMMARY:
==15746==    definitely lost: 12 bytes in 3 blocks
==15746==    indirectly lost: 42 bytes in 3 blocks
==15746==      possibly lost: 0 bytes in 0 blocks
==15746==    still reachable: 0 bytes in 0 blocks
==15746==         suppressed: 0 bytes in 0 blocks
==15746== Rerun with --leak-check=full to see details of leaked memory
==15746==
==15746== For counts of detected and suppressed errors, rerun with: -v
==15746== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 8)

How do you get Valgrind to show exactly where an error occured? I compiled my program (on a Windows machine over a Linux terminal via PuTTy) adding the -g debug option.

When I run Valgrind, I get the Leak and Heap summary, and I definitely have lost memory, but I never get information about where it happens (file name, line). Shouldn't Valgrind be telling me on what line after I allocate memory, it fails to deallocate later?

==15746==
==15746== HEAP SUMMARY:
==15746==     in use at exit: 54 bytes in 6 blocks
==15746==   total heap usage: 295 allocs, 289 frees, 11,029 bytes allocated
==15746==
==15746== LEAK SUMMARY:
==15746==    definitely lost: 12 bytes in 3 blocks
==15746==    indirectly lost: 42 bytes in 3 blocks
==15746==      possibly lost: 0 bytes in 0 blocks
==15746==    still reachable: 0 bytes in 0 blocks
==15746==         suppressed: 0 bytes in 0 blocks
==15746== Rerun with --leak-check=full to see details of leaked memory
==15746==
==15746== For counts of detected and suppressed errors, rerun with: -v
==15746== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 15 from 8)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

你的心境我的脸 2024-12-17 13:04:12

我已经多次对此感到困惑,并且无法弄清楚为什么“--leak-check=full”对我不起作用,所以我想我应该提出tune2fs评论。

最可能的问题是您(不是 ShrimpCrackers,而是现在正在阅读这篇文章的任何人)将 --leak-check=full 放在命令行末尾。 Valgrind 希望您在输入实际命令行来运行程序之前发布该标志。

即:

valgrind --leak-check=full ./myprogram

不是:

valgrind ./myprogram --leak-check=full

I've repeatedly gotten hosed on this, and couldn't figure out why '--leak-check=full' wasn't working for me, so I thought I'd bump up tune2fs comment.

The most likely problem is that you've (Not ShrimpCrackers, but whoever is reading this post right now) placed --leak-check=full at the end of your command line. Valgrind would like you to post the flag before you enter the actual command line to run your program.

i.e.:

valgrind --leak-check=full ./myprogram

NOT:

valgrind ./myprogram --leak-check=full
时光病人 2024-12-17 13:04:12

这不是与 valgrind 相关的选项。相反,必须使用 -g 选项编译代码,以保留调试符号。

cc -g main.c
valgrind --trace-children=yes --track-fds=yes --track-origins=yes --leak-check=full --show-leak-kinds=all ./a.out

It's not an option related to valgrind. Instead, the code have to be compiled with -g options, in order to preserve the debug symbol.

cc -g main.c
valgrind --trace-children=yes --track-fds=yes --track-origins=yes --leak-check=full --show-leak-kinds=all ./a.out
扛刀软妹 2024-12-17 13:04:12

尝试 valgrind --leak-check=full

这通常会打印更多有用的信息。
编译时还要添加 -O0 标志,这样您的代码就不会得到优化。

Try valgrind --leak-check=full

This normally prints more useful information.
Also add the -O0 flag when compiling so your code doesn't get optimized.

金兰素衣 2024-12-17 13:04:12

让我对其他读者更具体一些(我也有同样的问题,但我的论点顺序正确):
我发现 valgrind 需要可执行文件的路径,如果你不提供这个路径,那么它会运行,但不会给你行号。
就我而言,可执行文件位于不同的目录中,该目录位于我的 PATH 中,但要获取行信息,您必须运行

valgrind --leak-check=full path_to_myprogram/myprogram

Let me be more specific for other readers (i had the same problem but my arguments were in the right order):
I found out that valgrind needs the path to the executable, if you dont give this then it will run bu it won't give you the line numbers.
In my case the executable was in a different directory, which was in my PATH, but to get the line information you have to run

valgrind --leak-check=full path_to_myprogram/myprogram
樱&纷飞 2024-12-17 13:04:12

为了让 valgrind 显示文件中发生错误的行,
我必须将 -g 添加到编译命令的 END 中。

例如:

gcc -o main main.c -g

然后运行 ​​valgrind:

valgrind --leak-check=full --show-leak-kinds=all --track-origins=yes ./main

In order for valgrind to show the lines where the errors occurred in the file,
I had to add -g to the END of my compile command.

For Example:

gcc -o main main.c -g

Then just run valgrind:

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