[英]C++ static classes & shared_ptr memory leaks
我不明白为什么以下代码会产生内存泄漏(我在静态类实例中使用boost::shared_ptr
)。 有人可以帮我吗?
#include <crtdbg.h>
#include <boost/shared_ptr.hpp>
using boost::shared_ptr;
#define _CRTDBG_MAP_ALLOC
#define NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
static struct myclass {
static shared_ptr<int> ptr;
myclass() {
ptr = shared_ptr<int>(NEW int);
}
} myclass_instance;
shared_ptr<int> myclass::ptr;
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF |
_CRTDBG_CHECK_ALWAYS_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
return 0;
}
这是内存泄漏。 您正在初始化myclass的静态实例myclass_instance。 您还正在初始化“ shared_ptr myclass :: ptr”。
根据Stroustrup [3],按定义的顺序初始化静态变量。 因此,您具有myclass_instance的静态定义,该定义初始化构造时的内部ptr。 但是,您然后有了静态myclass :: ptr的定义,该定义调用了shared_ptr的默认构造函数。
这是经典静态排序问题的一个示例。 编译器认为myclass :: ptr实际上并未初始化,因此不会破坏原始的shared_ptr。 相反,它只是泄漏。
您将需要某种裸露的指针。 如果您使用的是C ++ 11,则可以使用三元赋值语句执行Nifty Counter Technique,如果您确定该对象已经初始化,则该语句会自动执行。 这很粗糙,但是可以。
这是我在C ++ 11中的处理方式:
#include <crtdbg.h>
#include <memory>
using std;
#define _CRTDBG_MAP_ALLOC
#define NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
// Note that the count could also be a field in an initializer static used in the Nifty Counter
// Technique covered in many texts.
static int count = 0; // This gets implicitly initialized to 0 by the executable load into memory.
static struct myclass {
static shared_ptr<int> ptr;
myclass() {
if (count++ == 0) {
ptr = make_shared<int>(0); //initialization
}
}
} myclass_instance;
shared_ptr<int> myclass::ptr = count == 0 ? make_shared<int>(0) : move(myclass::ptr);
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF |
_CRTDBG_CHECK_ALWAYS_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
return 0;
}
有关更多信息,请参见以下内容:
最有可能在全局对象被销毁之前检测到泄漏,并且shared_ptr
有机会释放该对象,因此很可能是错误的泄漏。
猜测CRT报告的是误报-以下代码说明了共享指针至少在g ++上正常工作
#include <iostream>
#include "boost/shared_ptr.hpp"
using namespace std;
using namespace boost;
struct R {
R() {
cerr << "ctor" << endl;
}
~R() {
cerr << "dtor" << endl;
}
};
struct A {
static shared_ptr<R> ptr;
A() {
ptr = shared_ptr<R>(new R);
}
};
shared_ptr<R> A::ptr;
static A a;
int main() {
}
它打印:
ctor
dtor
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.