Boost logo

Boost :

From: Howard Hinnant (hinnant_at_[hidden])
Date: 2002-09-30 15:29:22

On Monday, September 30, 2002, at 02:56 PM, Peter Dimov wrote:

>> Interesting example. A possible (but not necessarily better)
>> alternative which keeps the interfaces decoupled is:
>> px.reset(px2.get());
>> px2.release();
> No, this doesn't work. px.reset(X*) always takes ownership, even if an
> exception is thrown:
> px.reset(new X); // no leak, object deleted even if reset throws
> and in our case above px2 will attempt to double-delete "its" pointer
> when
> reset throws.

Ah, right. Serves me right for not glancing at shared_ptr.hpp before

>> I've just reviewed my own code and found that I've used this pattern
>> several times when transferring ownership out of a local auto_ptr-like
>> type.
> So do I, but only in cases where the (implementation detail) function
> doesn't take ownership when it throws, and such functions are usually
> a bad
> idea, except at a certain library level where you live, and I visit
> regularly. ;-)

Actually all of my "take ownership functions" are nothrow, but yes, we
are talking about relatively low level code (e.g. implementing insert
for node-based containers).

Pseudo code:

auto_ptr<node> ap(new node);
// do throwing stuff such as construct value_type into node
insert_node(ap.release()); // nothrow
// container owns node here


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