[英]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.