Boost logo

Boost :

From: David Abrahams (david.abrahams_at_[hidden])
Date: 2002-01-17 17:22:28

----- Original Message -----
From: "Gary Powell" <powellg_at_[hidden]>

> std::auto_ptr<type1> p1 = new type1(...);
> // Use p1
> transfer_to_new_owner(p1.release());
> Now you're safe, so long as transfer_to_new_owner is doing its job.
> <<
> Shouldn't this be:
> std::auto_ptr<type1> p1 = new type1(...);
> // Use p1
> transfer_to_new_owner(p1.get());
> p1.release();
> If transfer_to_new_owner(type1 *) throws before it actually takes over
> ownership of p1, your example leaks p1.
> Of course if it throws after it takes ownership, you're still in big
> trouble.

Yes, if transfer_to_new_owner(type1*) takes ownership and then throws, your
example double-deletes p1. Which is worse?

The point is really that when dealing with legacy interfaces on raw
pointers, you have to know something more than that an exception has
occurred: you need to know whether, in the event of an exception,
transfer-of-ownership has occurred or not. If I don't know anything, I'll
tend to opt for the first idiom because double-deletion is usually deadlier
than a leak... but of course that's a shot in the dark.

Boost list run by bdawes at, gregod at, cpdaniel at, john at