Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2002-09-30 13:48:21


From: "Peter Dimov" <pdimov_at_[hidden]>

> From: "Howard Hinnant" <hinnant_at_[hidden]>
> > > shared_ptr's case is different, as its reset is allowed to throw:
> > >
> > > shared_ptr<X> px;
> > > auto_ptr<X> px2;
> > >
> > > px = px2; // strong guarantee, px2 unchanged if reset throws
> > > px.reset(px2.release()); // px2 NULL, object destroyed if reset
throws
> >
> > 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.

Even if it weren't that way, it's a bad idea from a maintenance perspective
to have two objects which think they have sole ownership of the same
resource at one time. Eventually someone could come along and insert a
throwing operation.

-----------------------------------------------------------
           David Abrahams * Boost Consulting
dave_at_[hidden] * http://www.boost-consulting.com


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk