8) The aliasing constructor: constructs a shared_ptr which shares ownership information with r, but holds an unrelated and unmanaged pointer ptr. Even if this shared_ptr is the last of the group to go out of scope, it will call the destructor for the object originally managed by r. However, calling get() on this will always return a copy of ptr. It is the responsibility of the programmer to make sure that this ptr remains valid as long as this shared_ptr exists, such as in the typical use cases where ptr is a member of the object managed by r or is an alias (e.g., downcast) of r.get()
To my understanding, the usage of the aliasing constructor in example8 induces a memory leak as the
my_refcounting_api
pointerplugin
does not get freed by this kind of shared_ptr:How about some auxiliary wrapper:
Then use it along those lines:
I am not sure if this really THE way to go. But according to my understanding, this at least does not leak memory.