设置返回值时忽略"still reachable"
Ignore "still reachable" when setting return value
在 CI 系统中,我使用 valgrind 运行了一系列测试,如果 valgrind 没有发现错误,我希望返回值0
,否则1
。测试本身成功运行并返回0
.
这就是error-exitcode
似乎的目的:
--error-exitcode=<number> exit code to return if errors found [0=disable]
现在我有一个从第三方库中生成still reachable
的程序。不理想,但没关系。我尝试通过调用来定义still reachable
不是错误:
valgrind --errors-for-leak-kinds=definite,indirect,possible --error-exitcode=1 ./tests
哪些打印
==9198== LEAK SUMMARY:
==9198== definitely lost: 0 bytes in 0 blocks
==9198== indirectly lost: 0 bytes in 0 blocks
==9198== possibly lost: 0 bytes in 0 blocks
==9198== still reachable: 392 bytes in 4 blocks
==9198== suppressed: 0 bytes in 0 blocks
但仍然返回1
.
有没有办法忽略返回值中的still reachable
?
TL;DR:使用
valgrind --leak-check=full --error-exitcode=1 ./tests
好的,我想我在构建SSCCE时找到了答案
SSCCE
让memLeakTest.cpp
成为
#include <cstdlib>
#include <iostream>
void makeDefinitelyLostPointer()
{
int* definitelyLostPointer = new int(5555);
(*definitelyLostPointer) += 1;
}
void makeStillReachablePointer()
{
int* stillReachablePointer = new int(3333);
std::exit(0);
(*stillReachablePointer) += 1;
}
int main()
{
//makeDefinitelyLostPointer();
makeStillReachablePointer();
return 0;
}
- 运行
g++ memLeakTest.cpp -o memLeakTest
- 运行
./memLeakTest; echo $?
,显示返回值0
- 运行
valgrind ./memLeakTest; echo $?
,返回0
- 运行
valgrind --leak-check=full --error-exitcode=1 ./memLeakTest
,如果禁用makeDefinitelyLostPointer()
则返回0
,如果启用了makeDefinitelyLostPointer()
则返回1
。在这两种情况下,仍然有可访问量。