----- Mail original -----
De: "Edward Diener"
À: boost-users@lists.boost.org
Envoyé: Dimanche 25 Mars 2012 21:37:55
Objet: [Boost-users] [shared_ptr] Multiple enable_shared_from_this in inheritance tree
There was a Code Project article
(http://www.codeproject.com/Articles/286304/Solution-for-multiple-enable_shar...)
about a problem using multiple enable_shared_from_this derivations in
an
inheritance tree.
Does this problem actually exist in the current Boost implementation
?
I've been trapped by this two years ago, and now under 1.48 code below throws under VS2010 when calling B::shared_from_this().
// BEGIN
#include
#include
struct A : boost::enable_shared_from_this <A>
{};
struct B : boost::enable_shared_from_this <B>
{};
struct C : A, B
{};
int main()
{
boost::make_shared <C>()->A::shared_from_this();
boost::make_shared <C>()->B::shared_from_this();
return 0;
}
// END
weak_this_ is null so you have a bad_weak_ptr exception in shared_count::shared_count( weak_count const & r ).
Root problem seems to be that template function sp_enable_shared_from_this is only called for first matching specialization.
Following code does not throw : (*DISCLAIMER* this could make your computer explode, and is probably utterly wrong anyway)
boost::shared_ptr <C> c = boost::make_shared <C>();
c->A::shared_from_this();
boost::detail::sp_enable_shared_from_this (&c, c.get(), static_cast (c.get()));
c->B::shared_from_this();
Regards,
Ivan