|
Boost Users : |
Subject: Re: [Boost-users] Problem with enable_shared_from_this and shared_ptr
From: Michael Caisse (boost_at_[hidden])
Date: 2010-07-20 22:23:14
Ryan McConnehey wrote:
> If class "Pool" inherits from enable_shared_from_this the
> documentation says I'm able to do the following.
>
> shared_ptr<Pool> aPool = shared_from_this();
>
> The minimal code I've included shows that this isn't happening. The
> code compiles but I get this error "tr1::bad_weak_ptr". What should I
> be doing different?
>
> Ryan
>
>
> [CODE]
>
> //C++ Includes
> #include <exception>
> #include <iostream>
> #include <vector>
> #include <algorithm>
>
> //Boost Includes
> #include <boost/shared_ptr.hpp>
> #include <boost/enable_shared_from_this.hpp>
>
> class pool : public boost::enable_shared_from_this< pool > {
> private:
> using boost::enable_shared_from_this< pool >::shared_from_this;
>
> public:
> boost::shared_ptr<int> get(void) {
> boost::shared_ptr<pool> aPool = shared_from_this(); //This is
> where the tr1::bad_weak_ptr happens.
Ryan -
See the documentation here:
<http://www.boost.org/doc/libs/1_43_0/libs/smart_ptr/enable_shared_from_this.html>
There must be at least one shared_ptr instance that owns the object that
you are calling shared_from_this() on. Also see the examples at the
above link.
hth -
michael
-- ---------------------------------- Michael Caisse Object Modeling Designs www.objectmodelingdesigns.com
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