c++ - gperftools 中的明显内存泄漏

标签 c++ memory-leaks malloc gperftools

在运行一个使用地址清理器构建的程序时出现这个问题,这让我很好奇。

The gperftools source code包含以下功能:

void MallocExtension::Register(MallocExtension* implementation) {
  InitModule();
  // When running under valgrind, our custom malloc is replaced with
  // valgrind's one and malloc extensions will not work.  (Note:
  // callers should be responsible for checking that they are the
  // malloc that is really being run, before calling Register.  This
  // is just here as an extra sanity check.)
  if (!RunningOnValgrind()) {
    current_instance = implementation;
  }
}

InitModule定义如下

static void InitModule() {
  if (current_instance != NULL) {
    return;
  }
  current_instance = new MallocExtension; // pointless?
#ifndef NO_HEAP_CHECK
  HeapLeakChecker::IgnoreObject(current_instance);
#endif
}

我们的地址清理程序(当然不是 valgrind)提示 MallocExtension 对象的内存泄漏。显然,这是对的。但是为什么这个分配首先在那里?

我不认为开发自己的内存分配器的人会犯这种微不足道的错误。还有一个针对 valgrind 的显式检查。那么分配的目的是什么?

最佳答案

是的,在各种谷歌代码(即不仅仅是 gperftools)中故意泄漏在启动时分配的单例对象是很常见的。思路既不是初始化也不是销毁顺序是明确的。因此,试图在进程关闭时释放此类单例需要各种 super 难以跟踪的问题。

更多信息:https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables

关于c++ - gperftools 中的明显内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48440340/

相关文章:

c++ - 如何在一个函数中拥有所有选项?

java - Java ImageIO.read() 中的内存泄漏

C malloc 函数抛出内存错误

c - `malloc()` 刚刚分配的内存内容是什么?

C++容器,在析构时自动删除元素

c++ - 无法初始化QMainWindow类型的参数:ui-> setupUi(this)error

c++ - QML/D 文本编辑器和基本注意事项

c# - 如何防止 CompileAssemblyFromSource 泄漏内存?

javascript - 游戏循环内存泄漏

c - 如何处理 malloc 失败并返回 NULL?