Executing blocking ()
According to boost:
To access an object, a weak_ptr can be converted to a shared_ptr using the shared_ptr constructor or a member function lock.
Again, from boost:
shared_ptr<T> lock() const;
//Returns: expired()? shared_ptr<T>(): shared_ptr<T>(*this).
As I understand it, returning shared_ptr<T>(*this)
means creating a new shared_ptr with reference number 1; And this is definitely not what we want. So I guess I don't get it right. Can anyone explain? Thank you!
source to share
No, this is actually the point of shared_ptr - the copied instance will point to the same underlying data and increment the reference count for both instances.
This means it shared_ptr<T>(*this)
will create an additional instance shared_ptr
pointing to the same data and increment the reference count for both the this
new instance.
This is really more difficult in real code, as the original data is shared_ptr
available through the instance weak_ptr
, but in fact the original data is shared_ptr
split at the end (with the total number of references bumped into all existing copies of the particular object shared_ptr
).
source to share