Boost logo

Boost :

From: David Abrahams (abrahams_at_[hidden])
Date: 2001-04-06 07:09:39


----- Original Message -----
From: "Peter Dimov" <pdimov_at_[hidden]>

> > my_any_pair(shared_ptr<string>(auto_ptr<string>(new string("text") ),
> > auto_ptr<string>(new string("text2") );
> >
> > where I don't really want to have default construction, and then do the
> > assignment the "safe" way.
>
> Again, you don't need the auto_ptr. shared_ptr<string>(new string("text"))
> is guaranteed not to leak. If you successfully enter the constructor,
> shared_ptr takes responsibility for the pointer.
>
> The safe way to write the above is
>
> shared_ptr<string> p1(new string("text"));
> shared_ptr<string> p2(new string("text2"));
>
> my_any_pair(p1, p2);
>
> or alternatively you need shared_ptr_new:
>
> my_any_pair(shared_ptr_new<string>("text"),
> shared_ptr_new<string>("text2"));

I'm sure you know this, but maybe Gary doesn't:

    boost::shared_ptr<string>(new string("text"))

isn't any safer than

    std::auto_ptr<string>(new string("text"))

in the sense that any expression (e.g. function invocation) with two such
sub-expressions (e.g. parameters) can leak.

-Dave

P.S. When I get a few minutes maybe I'll expand the
boost/more/error_handling.html page (not yet released; available via CVS) to
discuss auto_ptr and Peter's non-const& technique. I guess resource
management should be added to the title somehow.


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