[prev in list] [next in list] [prev in thread] [next in thread] 

List:       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 () wanadoo ! fr>
Date:       2012-10-24 19:44:48
Message-ID: 50884530.2020005 () wanadoo ! fr
[Download RAW message or body]

Le 24/10/12 13:21, David Hagood a =E9crit :
> 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

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/bo=
ost
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic