简体   繁体   English

如果仍有可达的alloc,如何使valgrind报告错误

[英]How to make valgrind report an error when there are still reachable allocs

I'm writing a compiler that produces C code. 我正在编写一个生成C代码的编译器。 The programs produced consist only of the main function, and they use a lot of memory, that is allocated with malloc(). 生成的程序只包含main函数,它们使用大量内存,用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. 我很高兴,如果valgrind会向我报告在程序结束时内存不是free()d,即仍然可以访问的内存。 I'm using valgrind with --error-exitcode=1 inside a Makefile, to check for this kind of problem automatically. 我在Makefile中使用带有--error-exitcode = 1的valgrind来自动检查这种问题。

The question is: is there a way to make valgrind exit with 1 in case there are still reachable allocs? 问题是:有没有办法让valgrind退出1,以防仍有可达的分配?

The poroper options to use to exit with error when there is a reachable block at exit: 当退出时存在可到达的块时,用于退出并出错的poroper选项:

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

From Valgrind manual : 来自Valgrind手册

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. 这个问题的答案会影响ERROR SUMMARY行中打印的数字,以及--error-exitcode选项的效果。 First, a leak is only counted as a true "error" if --leak-check=full is specified. 首先,如果指定了--leak-check = full,则泄漏仅计为真正的“错误”。 Then, the option --errors-for-leak-kinds= controls the set of leak kinds to consider as errors. 然后,选项--errors-for-leak-kinds =控制泄漏类型集合作为错误。 The default value is --errors-for-leak-kinds=definite,possible 默认值为--errors-for-leak-types =明确,可能

The valgrind manual says: valgrind手册说:

Indirectly lost and still reachable blocks are not counted as true "errors", even if --show-reachable=yes is specified and they are printed; 即使指定了--show-reachable = yes并且打印了它们,间接丢失且仍然可以访问的块也不会被视为真正的“错误”; 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. 我发现无法使valgrind报告“仍然可以访问”为错误。 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. 这似乎是你唯一的选择(除了修补valgrind)是捕获valgrind的输出并解析“仍然可达”的行。

An alternative to grepping through Valgrind output: modify your compiler so it emits: 通过Valgrind输出的替代方法:修改编译器以使其发出:

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; 可能更好的转型:不要在你的主要部门叫exit(0) ; change it to return 0; 改变它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. 净效果应与上面相同 - __libc_main现在将为您调用exit ,并且main所有局部变量将在此时超出范围。

或者,您可以在makefile中使用一个小的shell脚本来grep通过valgrind的输出日志并相应地退出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM