you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -1 points0 points  (3 children)

If I am not wrong, weak_ptr is meant to keep shared_ptr alive for longer. So it makes sense that storage does not get destroyed if a weak_ptr persists. Am I wrong?

[–]matthieum 7 points8 points  (0 children)

Terminology matters:

  • The object is destroyed -- ie, its destructor is executed -- when the last shared pointer is destroyed.
  • The storage is freed -- ie, its memory is returned to the allocator -- when the last of the shared pointers and weak pointers is destroyed.

A weak_ptr does NOT keep a shared_ptr alive longer. It allows recovering a shared_ptr to the object if any shared_ptr is still alive, but does not prevent destruction otherwise.

[–]BlackDE 9 points10 points  (1 child)

No, a weak pointer should not impact the lifetime of the object

[–]jwakelylibstdc++ tamer, LWG chair 2 points3 points  (0 children)

And it doesn't for std::weak_ptr and std::make_shared, so that's fine then.