如何让 Valgrind 显示行错误?
如何让 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我已经多次对此感到困惑,并且无法弄清楚为什么“--leak-check=full”对我不起作用,所以我想我应该提出tune2fs评论。
最可能的问题是您(不是 ShrimpCrackers,而是现在正在阅读这篇文章的任何人)将 --leak-check=full 放在命令行末尾。 Valgrind 希望您在输入实际命令行来运行程序之前发布该标志。
即:
不是:
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.:
NOT:
这不是与 valgrind 相关的选项。相反,必须使用
-g
选项编译代码,以保留调试符号。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.尝试 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.让我对其他读者更具体一些(我也有同样的问题,但我的论点顺序正确):
我发现 valgrind 需要可执行文件的路径,如果你不提供这个路径,那么它会运行,但不会给你行号。
就我而言,可执行文件位于不同的目录中,该目录位于我的 PATH 中,但要获取行信息,您必须运行
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 显示文件中发生错误的行,
我必须将 -g 添加到编译命令的 END 中。
例如:
然后运行 valgrind:
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:
Then just run valgrind: