这个问题是在运行一个使用地址消毒剂构建的程序时出现的,这让我很好奇。
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对象的内存泄漏。显然,这是对的。但是,为什么这种分配首先在那里呢?
我不认为开发自己的内存分配器会犯这样一个微不足道的错误。此外,还有一个明确的检查,反对伐兰。那么,分配的目的是什么呢?
发布于 2018-02-03 22:09:03
是的,在谷歌的各种代码(不仅仅是gperftools)中,故意泄露启动时错误的单例对象是很常见的。这种思维既不是初始化的,也不是毁灭的顺序是明确的.因此,试图在进程停工时释放这样的单身汉,需要各种超级困难的问题来跟踪。
这里有更多信息:变量
https://stackoverflow.com/questions/48440340
复制相似问题