Boost logo

Boost :

Subject: Re: [boost] [smart_ptr] enable_shared_from_this and multiple inheritance solution
From: Philippe Cayouette (pcayouette_at_[hidden])
Date: 2011-01-03 17:29:08


On 2010-11-30 13:23, Philippe Cayouette wrote:
>>>> If anyone is interested in the code, it can be found at:
>>>>
>>>> http://208.106.110.44/~ansel/boost/enable_shared_from_polymorphic.patch
>>>>
>>>> This patch is against the current SVN version of boost.
>>
>> The only cast that occurs in this code is a static_cast that downcasts
>> from enable_shared_from_polymorphic<T> to T. I have read the relevant
>> portions of the standard several times and I beleive this is valid,
>> but I would definitely be curious if wiser heads agree with my reading.

I finally took the time to test your latest patch from the URL you
provided. I am using VS2005 right now and it does not work, in fact, the
function sp_enable_shared_from_this in your file never got called,
resulting in an uninitialized internal weak_ptr.

It builds successfully and even give the illusion that it works! I will
try to explain the problem. The function shared_from_this of the class
enable_shared_from_polymorphic recreates a shared_ptr using "this", the
weak_ptr you pass to the shared_ptr constructor is NULL and then
constructs a shared_ptr with px = this and pn.pi_ = NULL.

Now if you copy the shared_from_this into a second shared_ptr while the
original shared_ptr is still alive, the copy will work (this is the
illusion I was talking about). However, when the original shared_ptr
goes out of context, it is destroyed and if you use the copy shared_ptr
(still in context), you will work on deleted memory...

A good example is worth a million words. I have put a test file over there:
http://spoluck.ca:8000/boost/SharedFromThisTest.cpp

Debug step in it and you will see. You can set
USING_VARIADIC_AND_FACTORY_METHOD to 1 to fall back to my original
solution. You will need to have my original support file for that (still
at the same URL).
http://spoluck.ca:8000/boost/EnableSharedFromThis.h
http://spoluck.ca:8000/boost/SmartPtrBuilder.h

Did I used your patch erroneously?

Regards,

Philippe


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