Boost logo

Boost :

Subject: Re: [boost] Is there a way to specialize how a smart_ptr<T> releases the T?
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2012-10-24 15:44:48


Le 24/10/12 13:21, David Hagood a écrit :
> On 10/23/2012 09:16 PM, TONGARI wrote:
>> Just provide it with your custom Deleter in the ctor.
>>
>> template<class Y, class D> shared_ptr(Y * p, D d);
> OK, thanks.
>
> However - this does require every construction of a shared pointer to
> be passed that destructor - that seems error prone if it is known that
> all T must be released by a given call.
>
> If shared_ptr defined a release function:
>
> template <typename T> class shared_ptr
> {
> ...
> void Release(T *t) { delete(t);}
> }
> and used Release where it currently deletes the object, then in a case
> like mine, all you'd have do to is specialize it:
>
> template <> shared_ptr<xmlDoc>::Release(xmlDoc *t) { xmlFreeDoc(t);}
>
> and then all shared_ptr<xmlDoc> would correctly release the object.
>
> I guess what I can do to avoid the risk would be to derive from
> shared_ptr and provide the destructor in my ctor.
>
>
I see two options:

* either you warp a shared_ptr<xmlDoc,YourSpecificDeleter> and force the
constructor (the wrapper will have a lot of boiler plate code bat is
easy to do)
* you create a factory make_shared_xmlDoc that creates a
shared_ptr<xmlDoc,YourSpecificDeleter>. While this is not a complete
solution is easy to do and should solve most of the issues you are facing.

Best,
Vicente


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