Boost logo

Boost Users :

Subject: Re: [Boost-users] scoped_ptr::release (again)
From: Christopher Jefferson (chris_at_[hidden])
Date: 2009-02-24 09:53:15


On 23 Feb 2009, at 21:39, Richard Hadsell wrote:

> Alexander wrote:
>>> WFM...
>>>
>>> void foo(scoped_array<int> &b) {
>>> scoped_array<int> a(new int[5]);
>>> swap(a,b); // escape
>>> }
>>
>> Hm.. I agree with you that having swap to escape scoping but not
>> having release looks inconsequent. In my opinion there should be no
>> swap either.
> I use swap to do things like reallocate an array to a new size. I
> also use it to modify a linked list that uses scoped_ptr for its
> links.
>
> swap() still guarantees that some code will delete the object or
> array at some point. I think leaks are impossible.
>
> Would release() do that? I'm unfamiliar with its function. If it
> means that the code that released the pointer is now responsible for
> deleting it, I don't want it. It will just mean that I have to
> examine all the code to look for releases and then see whether I can
> prove that code is guaranteed to delete it.
>
> The best thing about scoped_ptr and _array is that they are almost
> foolproof. The only ways that I can think of to mess one up are (a)
> to use the wrong one for the type of allocation (object or array)
> and (b) to get() its pointer and then delete it.
>
> That's my opinion as a long-time user.

Could I suggest that if you want something like a scoped_ptr which you
can return from functions, and release, then you probably want the
slightly more powerful, but still very safe, unique_ptr?

I'd vote for keeping scoped_ptr as simple as possible, even removing
that swap, and directing people who want more power to unique_ptr.

Chris


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