忽略"仍然可以访问"设置返回值时

时间:2014-12-30 00:47:21

标签: c++ valgrind

在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

1 个答案:

答案 0 :(得分:0)

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;
}
  1. 运行g++ memLeakTest.cpp -o memLeakTest
  2. 运行./memLeakTest; echo $?,其中显示返回值0
  3. 运行valgrind ./memLeakTest; echo $?,返回0
  4. 运行valgrind --leak-check=full --error-exitcode=1 ./memLeakTest,如果0已停用,则返回makeDefinitelyLostPointer();如果1已启用,则返回makeDefinitelyLostPointer()。在这两种情况下仍然存在可达物。