Let's say we have
struct Foo {
shared_ptr<Foo> data;
}
int main() {
auto f1 = make_shared<Foo>();
auto f2 = make_shared<Foo>();
f1->data = f2;
f2->data = f1;
}
This has 2 memory locations on the heap: one for f1 and one for f2. There are 4 shared pointers in total. Each shared pointer has a "count" of 2. When f1 goes out of scope, it reduces the count of it by 1. It recursively calls the destructor of its "data" which is a shared pointer copy pointing at f2 which also reduces by 1. Likewise, when f2 goes out of scope, the same thing should happen. So the counts of both objects should become 0 leading their deletion. Is there a hole in my understanding?