gperftools中明显的内存泄漏

Apparent memory leak in gperftools

本文关键字:内存 泄漏 gperftools      更新时间:2023-10-16

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

gperftools源代码包含以下函数:

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对象的内存泄漏。显然,这是对的。但是,为什么首先会出现这种分配呢?

我拒绝认为开发自己的记忆分配器的人会犯这样一个微不足道的错误。还有对瓦尔格林德的明确检查。那么分配的目的是什么呢?

是的,在各种谷歌代码(即不仅仅是gperftools(中,故意泄漏启动时错误定位的单例对象是很常见的。思维既不是初始化,也不是破坏顺序是明确的。因此,试图在进程关闭时释放此类单例是要求各种超级难以跟踪的问题。

更多在这里: https://google.github.io/styleguide/cppguide.html#Static_and_Global_Variables