Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] shared_ptr<T> T destructor required even ifadeleter function is provided
From: vicente.botet (vicente.botet_at_[hidden])
Date: 2008-09-29 02:15:23


----- Original Message -----
From: "Peter Dimov" <pdimov_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Sunday, September 28, 2008 7:10 PM
Subject: Re: [boost] [smart_ptr] shared_ptr<T> T destructor required even
ifadeleter function is provided

>
> vicente.botet:
>> Hello,
>>
>> I have compiled the example Preventing delete px.get()and every thing is
>> OK until we use the reset function because shared_ptr<X> request that the
>> destructor of X must be public even if a deleter function is provided.
>> I'm wondering if this is a BUG on the smart pointer library
>
> ...
>
>> int main () {
>> shared_ptr<X> ptr = X::create();
>> ptr.reset(X::create().get());
>> return 0;
>> }
>
> shared_ptr is doing the right thing. The line
>
> ptr.reset(X::create().get());
>
> is a bug in your code (taking ownership of a pointer already managed by
> shared_ptr), and it is correct for it to not compile.

Hello Peter,

what about the following in which we don't transfer ownership. (Note that
now X::create() return X*)

int main () {
    shared_ptr<X> ptr(X::create(), X::deleter());
    ptr.reset(X::create());
    return 0;
}

Well, this was only to say that fact that shared_ptr don't have a deleter
template parameter at the class level, has as consequence that we check on
compile time that the delete function is public, and as consequence that the
destructor must be public. When the class has a factory and a deleter this
is not the case. So either my code has yet bug, either there is a bug on
shared_ptr class or you must add a constraint on the shared_ptr parameter
documentation (T::~T must be public)

#include <boost/shared_ptr.hpp>
using namespace boost;
class X {
private:
    ~X(){};
public:
    class deleter;
    friend class deleter;
    static X* create() {
        return new X;
    }
    class deleter {
    public:
        void operator()(X * p) { delete p; }
    };
};

I think that the problem is in the shared_count and sp_counted_impl_p which
do not have a deleter parameter, and so they use delete. But maybe I'm
missing something.

Best,

Vicente


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