Valgrind没有检测:“总堆使用:0个分配,0个释放,0个字节分配”

时间:2011-11-16 21:14:23

标签: c++ memory-leaks valgrind

即使在引入故意内存泄漏后,valgrind也会表示:

==13483== HEAP SUMMARY:
==13483==     in use at exit: 0 bytes in 0 blocks
==13483==   total heap usage: 0 allocs, 0 frees, 0 bytes allocated
==13483== 
==13483== All heap blocks were freed -- no leaks are possible

可执行文件是使用G ++ 4.1.2和4.6.2编译的:

g++ -ftemplate-depth-128 -O0 -fno-inline -Wall -pedantic -g -pthread -Wno-long-long -Wno-uninitialized <snipped definitions and include directives from the build system>

我尝试过使用Valgrind 3.5.0和3.6.1:

valgrind --leak-check=full --undef-value-errors=no --show-reachable=yes <executable args>

在我正在研究的库的框架内,我使用的是一个简单的测试用例:

#include "pwiz/utility/misc/Std.hpp"
#include "pwiz/utility/misc/Filesystem.hpp"
#include "pwiz/data/identdata/IdentDataFile.hpp"

using namespace pwiz::cv;
using namespace pwiz::data;
using namespace pwiz::identdata;
using namespace pwiz::util;

int main(int argc, char** argv)
{
    vector<string> args(argv+1, argv+argc);
    BOOST_FOREACH(const bfs::path& filename, args)
    {
        // intentional memory leak
        IdentDataFile* idp = new IdentDataFile(filename.string());
        IdentDataFile& id = *idp;
        cout << filename.string() << " "
             << id.analysisCollection.spectrumIdentification[0]->activityDate << " "
             << id.analysisCollection.spectrumIdentification[0]->spectrumIdentificationListPtr->spectrumIdentificationResult.size()
             << "\n";
    }
    return 0;
}

显然我不希望别人能够编译这个,但无论如何我怀疑它是关于库绊倒valgrind的东西所以一个更简单的测试用例是毫无意义的。我知道for循环正在执行,因为我在Valgrind执行期间得到了cout输出。如何在不进一步简化的情况下调试它?

1 个答案:

答案 0 :(得分:2)

实际上归结为链接器选项。我正在使用-static进行编译,因此valgrind没有机会替换自己的malloc实现。不幸的是,valgrind至少不会对此发出警告!