void foo()
{
my_scoped_ptr<someclass> p (new someclass());
my_scoped_ptr<someclass> q (p); // both p & q point to same my_p object
// may be an exception is raised
}
As the function ends (return or throw) and q
goes out of scope, the destructor for q
will delete
the someclass
object. Then immediately afterward, p
goes out of scope and its destructor will delete
the someclass
object again.
It is not valid to delete
the same pointer twice! (unless new
happened to return it again in the meantime).
[ And if any C++ implementation tried to make it valid, it would either have to link together all raw pointers with the same value (which is what shared_ptr
is for), or else keep track of what addresses have been deleted, which means they're still using memory of some sort and not really freed up. ]