Boost logo

Boost :

Subject: Re: [boost] [shared_ptr] Where is the bug?
From: Emil Dotchevski (emil_at_[hidden])
Date: 2008-10-03 17:29:37


On Fri, Oct 3, 2008 at 1:11 PM, vicente.botet <vicente.botet_at_[hidden]> wrote:
> ----- Original Message ----- From: "Emil Dotchevski"
>> On Fri, Oct 3, 2008 at 9:08 AM, vicente.botet <vicente.botet_at_[hidden]>
>> wrote:
>>>
>>> Yes, I know. I was not wondering about why the X's destructor is not run
>>> when deleting b, but why the X's destructor is called when deleting ptr.
>>
>> Because shared_ptr captures the destructor at the time it takes
>> ownership of the object. If this happens when the object is being
>> created (as in your example) the correct destructor is called even if
>> it isn't virtual.
>>
>> It is common for base types that are designed to be managed by
>> shared_ptr to use protected, non-virtual destructor (watch out for
>> retarded gcc warnings.)
>
> What is the advantage to use protected destructor when used with shared
> pointers?

As compared to a virtual destructor, calling a non-virtual destructor
is faster and could be inlined.

Making a destructor protected means that only the derived class
destructor is able to call it. Non-virtual destructor of a base type
shouldn't be public because calling it may lead to undefined behavior.

Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode


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