在CI系统中,我使用valgrind运行一系列测试,0如果valgrind没有找到错误,我期望返回值1.测试本身成功运行并返回0.
这error-exitcode似乎是为了:
--error-exitcode=<number> exit code to return if errors found [0=disable]
Run Code Online (Sandbox Code Playgroud)
现在我有一个still reachable从第三方库生成的程序.不理想,但没关系.我尝试still reachable通过调用来定义这不是错误:
valgrind --errors-for-leak-kinds=definite,indirect,possible --error-exitcode=1 ./tests
Run Code Online (Sandbox Code Playgroud)
打印
==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
Run Code Online (Sandbox Code Playgroud)
但仍然返回1.
still reachable在返回值中有没有办法忽略?
长话短说:使用
valgrind --leak-check=full --error-exitcode=1 ./tests
Run Code Online (Sandbox Code Playgroud)
好吧,我想我在构建 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;
}
Run Code Online (Sandbox Code Playgroud)
g++ memLeakTest.cpp -o memLeakTest./memLeakTest; echo $?,显示返回值0valgrind ./memLeakTest; echo $?,返回0valgrind --leak-check=full --error-exitcode=1 ./memLeakTest返回,如果启用则返回。在这两种情况下仍然可以到达。0makeDefinitelyLostPointer()1makeDefinitelyLostPointer()