【发布时间】:2020-02-28 16:33:37
【问题描述】:
我发现了几个相关的问题,但我找不到关于“如何”发生这种情况的解释。
我有以下代码,这比可以创建循环shared_ptr 引用问题的版本落后一步。
(在返回之前添加b.other = a;会导致问题)
为了更好地说明,我在重要的行添加了 cmets 以指示当时程序的状态。
#include <iostream>
#include <memory>
using namespace std;
struct Foo
{
explicit Foo(char i) : id{i} { cout << "Constructed " << id << endl; }
~Foo() { cout << "Destructed " << id << endl; }
shared_ptr<Foo> other = {};
char const id;
};
int main()
{
auto const a = make_shared<Foo>('a'); // a_use_count = 1, b_use_count = 0
auto const b = make_shared<Foo>('b'); // a_use_count = 1, b_use_count = 1
a->other = b; // a_use_count = 1, b_use_count = 2
return 0; // What happens now? Following is my "expectation" (which is wrong !)
// 1. destruct b => a_use_count = 1, b_use_count = 1
// 2. destruct a (in following order)
// 2.1 destruct a.other => b_use_count = 0 => show "Destructed b"
// 2.2 destruct a => a_use_count = 0 => show "Destructed a"
}
然而,事情并没有像我预期的那样发生。 a 和 b 以相反的顺序被破坏。我看到以下输出。
Constructed a
Constructed b
Destructed a
Destructed b
当程序返回上面时究竟会发生什么? (我希望理解这一点有助于理解循环依赖问题)
【问题讨论】:
-
显示的程序发生的事情并不是很复杂,但是它与循环依赖引起的潜在问题几乎无关,除非是非常基本的方式。
-
这个 Q 是专门针对 C++11 的吗?
-
@curiousguy 这是关于 C++11 及更高版本的,因为
std::shared_ptr之前不可用。
标签: c++ c++11 memory-management smart-pointers