为什么将智能指针重新分配给自身会导致破坏?
Why does reassigning a smart pointer to itself cause destruction?
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
Why does the line t_ptr = std::unique_ptr<Test>(t_ptr.get());
cause the destructor to be called?
因为unique_ptr
分配新对象时需要删除当前持有的对象。否则它会泄漏当前对象。但是,它不会检查新的是否实际上与当前的相同。如果你这样做,行为是未定义的。
Further, why am I able to continue calling methods after the supposed destruction?
因为这是未定义的行为。您正在对已删除的对象调用函数。当你这样做时会发生什么是不确定的。在你的系统上它可以工作,在我的系统上它崩溃了。
(题外话)
如果可以避免,我建议养成永远不要使用 new
的习惯,而是使用 std::make_unique
(或 std::make_shared
代替 shared_ptr
):
auto t_ptr = std::make_unique<Test>();
在某些情况下,当构造函数抛出时,异常安全是有好处的。基本上,至少从 2019 年开始,目前的经验法则是“不要使用 new
或 delete
”。“=21=”
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
Why does the line
t_ptr = std::unique_ptr<Test>(t_ptr.get());
cause the destructor to be called?
因为unique_ptr
分配新对象时需要删除当前持有的对象。否则它会泄漏当前对象。但是,它不会检查新的是否实际上与当前的相同。如果你这样做,行为是未定义的。
Further, why am I able to continue calling methods after the supposed destruction?
因为这是未定义的行为。您正在对已删除的对象调用函数。当你这样做时会发生什么是不确定的。在你的系统上它可以工作,在我的系统上它崩溃了。
(题外话)
如果可以避免,我建议养成永远不要使用 new
的习惯,而是使用 std::make_unique
(或 std::make_shared
代替 shared_ptr
):
auto t_ptr = std::make_unique<Test>();
在某些情况下,当构造函数抛出时,异常安全是有好处的。基本上,至少从 2019 年开始,目前的经验法则是“不要使用 new
或 delete
”。“=21=”