Boost logo

Boost :

Subject: [boost] [shared_ptr] enable_shared_from_any - patch & files
From: Philip Warner (pjw_at_[hidden])
Date: 2010-05-18 01:17:53


>
> Specifically, what I would like to see is a solution to the following code:
>
> C1 *p = new C1();
> shared_ptr<C1> ptr(p);
> ...
> ...some time later....
> shared_ptr<C1> ptr2<p>;
>
> and the resulting mess because of double-deallocation.
>
...
> I am very happy to provide a patch that:
>
> - adds a class, enable_shared_from_any, that can be inherited from
> - updates shared_ptr to work in the above code, if C1 inherits from
> enable_shared_from_any
>

If people are interested, the patch is attached. It includes a patch
file as well as final versions of the files.

'sp_enable_shared_from_this' now returns a value; for old-style
shared_ptr objects this is a constant 'false'. Allocation of the
shared_counter object has been deferred until the
'sp_enable_shared_from_this'; if the result is true, it is assumed that
the shared_counter has been allocated/copied. Otherwise, the
shared_counter is allocated in the constructor.

There used to be an extra 'sp_enable_shared_from_this'for each of the
possible base classes (enable_shared_from_this and
enable_shared_from_this2); now there are three for each possible base
class because we have to pass the various possible allocator/deallocator
parameters to the template functions.

There is scope here to remove both enable_shared_from_this and
enable_shared_from_this2 since, I believe, enable_shared_from_any is
capable of performing both the tasks that these were intended for, but I
have not done this. I will do so if people prefer. There are a number of
different ways this could be done for backward compatibility.

The patch is against 1.43.0.




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