【问题标题】:Valgrind is not instrumenting: "total heap usage: 0 allocs, 0 frees, 0 bytes allocated"Valgrind 没有检测:“总堆使用量:0 分配,0 释放,0 字节分配”
【发布时间】:2011-11-16 21:14:22
【问题描述】:

即使在引入了故意的内存泄漏 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 输出。如何在不进一步简化的情况下对其进行调试?

【问题讨论】:

  • 你会产生什么样的泄漏?
  • 故意泄露是一个简单的“new”语句,没有对应的“delete”。
  • 能否提供出处?你有没有可能在那里有自定义operator new()
  • idp 定义为IdentDataFileIdentDataFile *?
  • 你需要开始缩减你的源代码,直到你找到一个仍然可以证明问题的简单程序。例如,使用this program,valgrind 确实表明存在内存泄漏。 (至少在我的 Ubuntu 笔记本电脑上)。

标签: c++ memory-leaks valgrind


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-23
    • 2023-03-03
    • 2012-02-25
    • 2015-12-19
    • 2017-01-09
    • 2020-05-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多