Creating shared_ptr does not inherit magic powers on a pointee object. Magic is all in shared_ptr - and its copies; myself. If you stop using it, you will lose the reference count; worse because you used it at some point, the object will be automatically deleted if you do not expect it.
The whole point of having shared_ptr is that you know that your object will not be destroyed when you are still using it.
In the following:
T* foo() { shared_ptr<T> sp(new T()); return sp.get();
your pointer is immediately invalid.
There may well be circumstances in which you retrieve a raw pointer, but they should be rare. If you are in a function in which you know that you do not need reference counting, just pass shared_ptr by reference.
Lightness races in orbit
source share