小编Qwa*_*lly的帖子

尽管使用静态unique_ptr进行析构函数调用,但内存泄漏

unique_ptr用作静态数据成员来保存指针.

struct Test
{
    int i;
    ~Test()
    {
        cout << "destructed" << endl;
    }
};

struct S
{
    static unique_ptr<Test> te;
};
unique_ptr<Test> S::te = unique_ptr<Test>(new Test());
Run Code Online (Sandbox Code Playgroud)

程序终止S::te被破坏,调用Test-destructor.

但_CrtDumpMemoryLeaks向我显示内存泄漏,其内存位置S::te.get()是指向(破坏的)Test对象的指针.

我不明白这种行为.

我不能用静电unique_ptr吗?尽管unique_ptr实现调用析构函数,为什么会出现泄漏?

c++ static memory-leaks unique-ptr

4
推荐指数
1
解决办法
1369
查看次数

标签 统计

c++ ×1

memory-leaks ×1

static ×1

unique-ptr ×1