gperftools中的明显内存泄漏

杂技演员

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

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

我拒绝认为开发自己的内存分配器的人会犯这样一个小错误。还对valgrind进行了明确检查。那么分配的目的是什么?

阿里亚塞·坎德拉森卡(Aliaksei Kandratsenka)

是的,在各种Google代码(即不仅是gperftools)中,有意泄漏启动时分配的单例对象是很常见的。思想既没有明确定义初始化也没有破坏顺序。因此,尝试在进程关闭时释放此类单例将要求各种超级难度来跟踪问题。

此处更多内容:https : //google.github.io/styleguide/cppguide.html#Static_and_Global_Variables

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章