Actually, what you observed is wrong.
The point of smart pointer is to remove the responsibility of destroying the object. That means that the object will be delete when the reference count reach 0: it doesn't matter which pointer is destructed first.
In your case, this would be when my_ptr
goes out of scope (assuming you don't make and keep a copy in one your func()
.
This is what the reference counter should be:
{
sharedPtr<abc> my_ptr(new abc); //smart pointer created. Ref counter = 1.
func1(my_ptr); // Ref counter=2 while in func1
// Ref count is 1 again.
func2(my_ptr); // Ref counter=2 while in func2
// Ref count is 1 again.
func3(my_ptr); // Ref counter=2 while in func3
// Ref count is 1 again.
} // my_ptr goes out of scope, ref count reach 0, and abc is deleted.