Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shared_ptr's reference counting object needs to keep track of the number of weak_ptrs pointing to the object too?

Hi I am reading through this document and some other documents about C++'s shared_ptr and they all seem to suggest that apart from the number of shared_ptr pointing to the allocated object, the reference count object has to keep track of how many weak_ptr pointer pointing to the object as well. My question is why? From my understanding, weak_ptr is non-owning so if the count of shared_ptr pointing to the object reaches zero the object can be deleted. That is why sometimes we need to use expired to check the availability of an object pointed by a weak_ptr. Could you explain the reason for needing to keep track of the number of weak_ptrs?

Why do we need weak count here? enter image description here

like image 223
Bob Fang Avatar asked Jul 19 '17 19:07

Bob Fang


People also ask

What is a role of the reference counter in shared_ptr?

The shared reference counter counts the number of owners. Copying a std::shared_ptr increases the reference count by one. Destroying a std::shared_ptr decreases the reference count by one. If the reference count becomes zero, the resource will automatically be released.

What is the difference between shared_ptr and Weak_ptr?

The only difference between weak_ptr and shared_ptr is that the weak_ptr allows the reference counter object to be kept after the actual object was freed. As a result, if you keep a lot of shared_ptr in a std::set the actual objects will occupy a lot of memory if they are big enough.

What is the purpose of the shared_ptr <> template?

std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer.

Why do we need Weak_ptr?

By using a weak_ptr , you can create a shared_ptr that joins to an existing set of related instances, but only if the underlying memory resource is still valid. A weak_ptr itself does not participate in the reference counting, and therefore, it cannot prevent the reference count from going to zero.

Is it possible to pass shared pointers by constant reference?

In controlled circumstances you can pass the shared pointer by constant reference. Be sure that nobody is concurrently deleting the object, though this shouldn't be too hard if you're careful about to whom you give references.

How do you know if a shared pointer is empty?

the number of std::shared_ptr instances managing the current object or ​0​ if there is no managed object. comparison with ​0​. If use_count returns zero, the shared pointer is empty and manages no objects (whether or not its stored pointer is nullptr ).

Why do shared pointers keep objects alive in C++?

This gives it its intended semantics: Every scope that contains a copy of the shared pointer keeps the object alive by virtue of its "share" in the ownership.

What is the use_count value of a shared_ptr?

In multithreaded environment, the value returned by use_count is approximate (typical implementations use a memory_order_relaxed load) the number of std::shared_ptr instances managing the current object or ​0​ if there is no managed object. comparison with ​0​.


1 Answers

std::weak_ptr refers to the control block to know if the object still exists and if so, to provide a std::shared_ptr to it when needed. For that reason, the control block must exist as long as either a std::weak_ptr or a std::shared_ptr exists. You need to track the number of instances of std::weak_ptr to know when the last one is destroyed, just like for std::shared_ptr.

like image 162
François Andrieux Avatar answered Sep 21 '22 12:09

François Andrieux