mercredi 19 juillet 2017

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

Aucun commentaire:

Enregistrer un commentaire