Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] enable_shared_from_this and shared_ptr to a base class
From: Adam Romanek (romanek.adam_at_[hidden])
Date: 2013-06-24 06:46:10


On 06/24/2013 12:05 PM, Jonathan Wakely wrote:
> On 24 June 2013 10:22, Adam Romanek wrote:
>> On 06/24/2013 10:06 AM, Jonathan Wakely wrote:
>>
>> When taking inheritance into account one can safely say that a pointer to a
>> base class X pointing to an instance of a derived class Y owns this
>> instance. That's my point of view.
>
> No, that's not how shared_ptr's ownership works. It owns a pointer
> and when the last owner releases ownership the pointer is passed to
> the deleter (which might just call 'delete'). But there is no
> requirement that the pointer is non-null, or points to an instance of
> some specific type, and certainly no requirement that if the
> shared_ptr owns an X* that it has anything to do with any instance of
> a different type Y, even if Y is related to X by inheritance. The
> shared_ptr doesn't care about such things, it just owns a pointer and
> arranges for it to be passed to the relevant deleter at the
> appropriate time.

I get your point. I've never expected that shared_ptr would
(auto)magically "detect" any relation between X and Y or do anything
that is unrelated to its nominal functionality.

>
> In this example the four shared_ptr objects all share ownership of the
> same pointer of type Y*, even though one of them is a shared_ptr<X>
> and one is shared_ptr<void> and one stores a completely unrelated
> pointer type, but still owns a pointer of type Y*:
>
> struct X { virtual ~X() {} };
> struct Y : X { };
>
> std::shared_ptr<Y> sy(new Y);
> std::shared_ptr<X> sx(sy);
> std::shared_ptr<void> sv(sx);
> std::shared_ptr<int> si(sv, (int*)0);
>
> The ownership is a property of the static type of the pointer passed
> to the first shared_ptr constructor that creates a unique shared_ptr
> (i.e. one with use_count()==1). If you don't pass a Y* to the first
> constructor then it doesn't own a Y* and won't initialize any
> enable_shared_from_this<Y> base class.

That's something that is truly important in the case we're discussing.

In fact, the last paragraph makes things absolutely clear. This is what
I would expect to see in the docs if I was in trouble.

>
>
>> So maybe "ownership" should be described in more detail?
>
> Yes, if people are making up their own interpretations of it then
> maybe it should be.

WBR,
Adam Romanek


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