为什么将智能指针重新分配给自身会导致破坏?

问题描述 投票:1回答:1

TLDR

为什么行t_ptr = std::unique_ptr<Test>(t_ptr.get());会导致析构函数被调用?

这条线似乎无辜地将t_ptr归还给自己......

此外,为什么我能够在假定的破坏后继续调用方法?

示例代码

class Test
{
    public:
        Test()
        {
            printf("Constructor called: %p\n", this);
            i = 0;
        };
        void print()
        {
            printf("%d\n", i++);
        };
        ~Test()
        {
            printf("Destructor called: %p\n", this);
        };
    private:
        int i;
};

int main(int argc, char** argv)
{
    std::unique_ptr<Test> t_ptr = std::unique_ptr<Test>(new Test());
    t_ptr->print();
    t_ptr->print();
    t_ptr->print();
    t_ptr = std::unique_ptr<Test>(t_ptr.get());
    t_ptr->print();
    t_ptr->print();
    t_ptr->print();
};

输出是

Constructor called: 0x55c9811a1e70
0
1
2
Destructor called: 0x55c9811a1e70
0
1
2
Destructor called: 0x55c9811a1e70
c++ smart-pointers
1个回答
6
投票

为什么行t_ptr = std::unique_ptr<Test>(t_ptr.get());会导致析构函数被调用?

因为unique_ptr在为其分配新对象时需要删除它。否则会泄漏当前对象。但是,它不会检查新的实际上是否与当前的实际相同。如果这样做,则行为未定义。

此外,为什么我能够在假定的破坏后继续调用方法?

因为这是未定义的行为。您正在调用已删除的对象上的函数。当你这样做时会发生什么是未定义的。在你的系统上工作,在我的系统崩溃。

(偏离主题的旁注)

如果你可以避免使用new,并且使用std::make_unique(或std::make_shared用于shared_ptr),我建议养成永远不要使用auto t_ptr = std::make_unique<Test>(); 的习惯:

new

在构造函数抛出的某些情况下,有异常安全性的好处。基本上,至少从2019年开始,目前的经验法则是“不要使用delete或qazxswpoi”。

© www.soinside.com 2019 - 2024. All rights reserved.