Boost logo

Boost Users :

From: David Abrahams (dave_at_[hidden])
Date: 2008-08-17 15:36:13


on Sun Aug 17 2008, Christian Larsen <contact-AT-dword.dk> wrote:

> Hi,
>
> This is just make sure I'm not being too careful in this situation. So
> please confirm whether this is correct.
>
> I have a class with two scoped_ptr members, and I want to pass two
> pointers to the constructor, which will then transfer the ownership of
> both to the two member scoped_ptrs. I assume the following is not
> safe:
>
> (I didn't try compiling; using namespace boost and std.)
>
> struct B {};
>
> class A
> {
> public:
> A(B* b1, B* b2)
> : b1_(b1), b2_(b2)
> {}
> private:
> scoped_ptr<B> b1_;
> scoped_ptr<B> b2_;
> };
>
> // Create an A this way:
> A a(new B, new B);
>
> Is it correct that this could fail if one of the 'new B's throw, thus
> leaking before the constructor ever started?

Correct. Please see the guideline at
http://www.boost.org/doc/libs/1_36_0/libs/smart_ptr/shared_ptr.htm#BestPractices

> If that is so, is this then the correct way to achieve the same in a
> safe manner?
>
> struct B {};
>
> class A
> {
> public:
> A(auto_ptr<B>& b1, auto_ptr<B>& b2)
> : b1_(b1.release()), b2_(b2.release())
> {}
> private:
> scoped_ptr<B> b1_;
> scoped_ptr<B> b2_;
> };
>
> // Create an A this way:
> A a(auto_ptr<B>(new B), auto_ptr<B>(new B));

No, that has the same problem (your auto_ptr's aren't named). the 2nd
new could throw before any auto_ptrs are constructed, because the order
of evaluation is unspecified.

HTH,

-- 
Dave Abrahams
BoostPro Computing
http://www.boostpro.com

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