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_ptr
s?
Why do we need weak count here?
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.
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.
std::shared_ptr is a smart pointer that retains shared ownership of an object through a pointer.
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.
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.
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 ).
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With