当仍有可到达的分配时,如何使 valgrind 报告错误

发布于 2024-11-08 19:39:00 字数 278 浏览 0 评论 0原文

我正在编写一个生成 C 代码的编译器。生成的程序仅包含 main 函数,并且它们使用大量内存,这些内存是通过 malloc() 分配的。分配的大部分内存仅在程序的一小部分中使用,我认为在使用后释放它是一个好主意,因为它不会再次使用。那么,如果 valgrind 在程序结束时向我报告内存未 free()d,即仍然可达的内存,我会很高兴。我在 Makefile 中使用 valgrind 和 --error-exitcode=1 来自动检查此类问题。

问题是:有没有办法让 valgrind 以 1 退出,以防仍有可到达的分配?

I'm writing a compiler that produces C code. The programs produced consist only of the main function, and they use a lot of memory, that is allocated with malloc(). Most of the memory allocated is used only in a small part of the program, and I thought it would be a good idea to free() it after use, since it's not going to be used again. I would be glad, then, if valgrind would report to me about memory not free()d in the end of the program, that is, still reachable memory. I'm using valgrind with --error-exitcode=1 inside a Makefile, to check for this kind of problem automatically.

The question is: is there a way to make valgrind exit with 1 in case there are still reachable allocs?

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

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

发布评论

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

评论(4

你是暖光i 2024-11-15 19:39:00

通过 Valgrind 输出进行 grep 的另一种方法:修改编译器,使其发出:

int main() { return foo_main(); }
int foo_main() {  /* whatever you've emitted before */ }

假设您没有将分配的块分配给全局变量(这没有意义,因为您只有一个函数),您刚刚将“仍然可达”转换为“肯定泄露了”。

可能更好的转换:不要在 main 中调用 exit(0) ;将其更改为 return 0; 。最终效果应该与上面相同 - __libc_main 现在将为您调用 exit,并且 main 中的所有局部变量都将超出范围到那时。

An alternative to grepping through Valgrind output: modify your compiler so it emits:

int main() { return foo_main(); }
int foo_main() {  /* whatever you've emitted before */ }

Assuming you are not assigning allocated blocks to global variables (which would make no sense since you only have one function), you've just transformed "still reachable" into "definitely leaked".

Possibly even better transformation: don't call exit(0) in your main; change it to return 0; instead. The net effect should be same as above -- __libc_main will now call exit for you, and all local variables in main will be out of scope by that time.

⊕婉儿 2024-11-15 19:39:00

valgrind 手册 说:

间接丢失但仍可达
块不计为 true
“错误”,即使 --show-reachable=yes
已指定并打印;
这是因为这样的块不需要
由程序员直接修复。

我发现没有办法让 valgrind 报告“仍然可达”错误。似乎执行此操作的唯一选择(除了修补 valgrind 之外)是捕获 valgrind 的输出并解析“仍然可达”行。

The valgrind manual says:

Indirectly lost and still reachable
blocks are not counted as true
"errors", even if --show-reachable=yes
is specified and they are printed;
this is because such blocks don't need
direct fixing by the programmer.

I have found no way to make valgrind report "still reachable"s as error. It seems to be that your only option to do this (other than patching valgrind) is to capture the output of valgrind and parse the "still reachable" line.

夏花。依旧 2024-11-15 19:39:00

当退出时存在可到达块时,用于错误退出的 poroper 选项:

valgrind --tool=memcheck --leak-check=full --show-reachable=yes --errors-for-leak-kinds =all

来自 Valgrind 手册

由于存在不同严重程度的不同类型的泄漏,因此一个有趣的问题是:哪些泄漏应该被视为真正的“错误”,哪些不应被视为真正的“错误”?

这个问题的答案会影响错误摘要行中打印的数字,以及 --error-exitcode 选项的效果。首先,如果指定了 --leak-check=full,则泄漏仅被视为真正的“错误”。然后,选项 --errors-for-leak-kinds= 控制要视为错误的泄漏类型集。默认值为 --errors-for-leak-kinds=definite,possible

The poroper options to use to exit with error when there is a reachable block at exit:

valgrind --tool=memcheck --leak-check=full --show-reachable=yes --errors-for-leak-kinds=all

From Valgrind manual:

Because there are different kinds of leaks with different severities, an interesting question is: which leaks should be counted as true "errors" and which should not?

The answer to this question affects the numbers printed in the ERROR SUMMARY line, and also the effect of the --error-exitcode option. First, a leak is only counted as a true "error" if --leak-check=full is specified. Then, the option --errors-for-leak-kinds= controls the set of leak kinds to consider as errors. The default value is --errors-for-leak-kinds=definite,possible

小巷里的女流氓 2024-11-15 19:39:00

或者,您可以在 makefile 中包含一个小 shell 脚本,用于 grep valgrind 的输出日志并相应退出。

Alternatively you can have a small shell script in your makefile to grep through output logs of valgrind and exit accordingly.

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