|
Boost Users : |
Subject: Re: [Boost-users] [shared_ptr] Multiple enable_shared_from_this in inheritance tree
From: ivan.lelann_at_[hidden]
Date: 2012-03-26 04:39:04
----- Mail original -----
> De: "Edward Diener" <eldiener_at_[hidden]>
> Ã: boost-users_at_[hidden]
> 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_shared_from_this-in-i)
> 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 <boost/smart_ptr/enable_shared_from_this.hpp>
#include <boost/make_shared.hpp>
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 <boost::enable_shared_from_this<B>* > (c.get()));
c->B::shared_from_this();
Regards,
Ivan
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net