Reading this stackoverflow answer :
QWeakPointer – Do you sense a reoccurring pattern? Just as
std::weak_ptr and boost::weak_ptr this is used in conjunction with
QSharedPointer when you need references between two smart pointers
that would otherwise cause your objects to never be deleted.
My question is – could anybody explain me such situation on a simple example, when two referencing smart pointers could cause non-deleted objects?
Thank you in advance..
In the following example, neither of the
Sobjects will ever be destroyed, because the object pointed to byaowns the object pointed to byb, and vice-versa.std::weak_ptris used to break reference cycles. If object lifetime is known to extend beyond the lifetime of the non-owning pointer, raw pointers can be used as well.The same principles apply to Qt’s smart pointers, like
QWeakPointer.