This is the mail archive of the libstdc++@gcc.gnu.org mailing list for the libstdc++ project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [GSoC] __enable_shared_from_this_helper


On 03/05/15 19:03 -0400, Fan You wrote:
Follow up questions:

> According to my knowledge, __enable_shared_from_this construct a
> weak_ptr in order to safely instantiate a new shared_ptr use only this
> pointer. So, does __enable_shared_from_this_helper has no effect until
> user trying to do something like this?
>
>> class Test : public ___enable_shared_from_this { };
>>
>> shared_ptr<Test> (new Test());

Correct.
It ensures that when an object is owned by a shared_ptr, the weak_ptr
member shares ownership with that shared_ptr.

However,

ctor of __shared_count only pass the same type of pointer into

__enable_shared_from_this_helper(_M_refcount, __p, __p);


but __enable_shared_from_this_helper take two different types of ptr.

(__shared_count&, __enable_shared_from_this* ptr1, _Tp1* ptr2)

Can you give any specific example of using this feature ?

Look at how it's called.

The same pointer is passed to it twice. If the type is derived from
enable_shared_from_this then one pointer implicitly converts to a
pointer-to-base (which allows us to detect that the object is derived
from enable_shared_from_this) and the other pointer is the one that is
set in the weak_ptr (we can't use the first pointer for this because
it's already been converted to the base type).

2.
int* a = new int(1);
shared_ptr<int> a1(a);
shared_ptr<int> a2(a);

This is undefined behaviour.

When user are trying to do some thing like this, should it throw? I've
tested on the original
std::shared_ptr in gcc 4.9.2, it will not throw on type like <int> but
it do throw on array types.
However, my currently implementation will throw "double deletion" at runtime.

<http://stackoverflow.com/questions/10338606/multiple-shared-ptr-storing-same-pointer>

That's one possible result of undefined behaviour. There is no need to
detect it and handle it though.


3. Also, we might also need a specialization for
enable_shared_from_this in order to use
shared_from_this() with std::experimental::shared_ptr;

Yes, maybe, I haven't thought about how enable_shared_from_this needs
to change.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]