Boost logo

Boost Users :

From: Rene Rivera (grafikrobot_at_[hidden])
Date: 2006-08-02 15:34:00


Milutin Jovanovic wrote:
> I finally decided to switch to boost from my own implementation of some
> helper classes, and I noticed that scoped_ptr and scoped_array in boost lack
> the release method(). I had to fall back to std::auto_ptr.
>
> However, this seems like such a basic and fundamental problem that I have
> started to fear that I am missing something obvious... The usage I got used
> to was something like this:
>
> {
> scoped_ptr<A> temp( new A() );
> verify( temp.get() ); // might throw
> store_in_a_very_special_place( temp.get() ); // might throw; and I
> should keep ownership if thrown
> temp.release();
> }
>
> I though this was good practice to make code exception safe. Sure I could do
> it with try-catch but that gets messy if I allocate multiple instances etc.

Try writing it as:

{
     shared_ptr<A> temp( new A() );
     verify( temp ); // might throw
     store_in_a_very_special_place( temp ); // might throw; and I
should keep ownership if thrown
}

It's less code and it's safer. But if you must use scoped_ptr another
alternative would be:

{
     scoped_ptr<A> temp( new A() );
     verify( *temp ); // might throw
     store_in_a_very_special_place( *temp ); // might throw; and I
should keep ownership if thrown
}

> So the question is:
>
> 1) is there something wrong with the concept of release()?
> 2) if release() is not evil, is there any other reason why is it not in
> boost::scoped_*?

See the FAQ for this at the bottom of the scoped_ptr docs
<http://boost.org/libs/smart_ptr/scoped_ptr.htm>.

-- 
-- Grafik - Don't Assume Anything
-- Redshift Software, Inc. - http://redshift-software.com
-- rrivera/acm.org - grafik/redshift-software.com
-- 102708583/icq - grafikrobot/aim - grafikrobot/yahoo

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net