The call is error. Unlike ex12.10, using p.get()
to initialize the parameter ptr
of function process
creates a new shared_ptr
which is independent of the original shared_ptr
, but sharing the same underlying memory. Those two pointers have no awareness of each other even though they point to the same memory. Thus, before the end of function scope, the newly created ptr
is destoryed, and according to its reference count, it is the only shared_ptr
pointing the memory, the memoried is freed. However, the pointer p
in the caller function still points to that memory. So the call is error.