|
Boost Users : |
Subject: Re: [Boost-users] [shared_ptr] Multiple enable_shared_from_this in inheritance tree
From: ivan.lelann_at_[hidden]
Date: 2012-03-27 05:08:56
----- Mail original -----
> De: "Edward Diener" <eldiener_at_[hidden]>
> Ã: boost-users_at_[hidden]
> Envoyé: Lundi 26 Mars 2012 23:54:21
> Objet: Re: [Boost-users] [shared_ptr] Multiple enable_shared_from_this in inheritance tree
>
> On 3/26/2012 4:39 AM, ivan.lelann_at_[hidden] wrote:
> >
> >
> > ----- 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();
> >
>
>
> My case was initially that I had a base class deriving from
> enable_shared_from_this<> and another class deriving from that base
> class and its own enable_shared_from_this<>.
Precisely the issue I got when I discovered the problem.
>
> My main point is that if having more than one
> enable_shared_from_this<>
> in the inheritance hierarchy normally fails, this should be
> documented
> with the proper information on how to workaround the problem.
>
I agree.
I wondered how current C++11 standard libraries handled this.
With VS2010, replacing "boost" with "std" made both "shared_from_this()" calls throw.
(with two esft bases the compiler chooses a dummy specialization)
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