Problem with weak_ptr exception

I'm having some problems with bad_weak_ptr exceptions. I have reduced my
code into this test case:
#include

I can't make sense of your code ("weakThis" in both A and B, even though B is derived from A?), but have a look at boost::shared_from_this Am Wednesday 28 October 2009 12:43:08 schrieb Jacob Carlborg:
I'm having some problems with bad_weak_ptr exceptions. I have reduced my code into this test case:
#include
#include using boost::shared_ptr; using boost::weak_ptr;
class A { weak_ptr<A> weakThis;
public: virtual ~A(){}
shared_ptr<A> a;
shared_ptr<A> getThis () { shared_ptr<A> x(weakThis); return x; }
void init () { a = getThis(); }
static shared_ptr<A> create () { shared_ptr<A> x(new A);
x->weakThis = x; x->init();
return x; } };
class B : public A { weak_ptr<B> weakThis; public: virtual ~B(){}
shared_ptr<B> getThis () { shared_ptr<B> b(weakThis); return b; }
static shared_ptr<B> create () { shared_ptr<B> x(new B); x->weakThis = x; x->init();
return x; } };
int main (char** argv, int argc) { shared_ptr<B> b = B::create();
return 0; }
If I create an instance of A instead of B or doesn't call x->init() in the B's create function I don't get an exception. The problem is creating a shared_ptr from the weak_ptr in getThis.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

On 10/28/09 15:56, Stefan Strasser wrote:
I can't make sense of your code ("weakThis" in both A and B, even though B is derived from A?), but have a look at boost::shared_from_this
If I don't have a weak_ptr in B I get this error when trying to create a shared_ptr<B> from a weak_ptr<A>: /shared_ptr.hpp:207: error: invalid conversion from 'A* const' to 'B*' I've already tried boost::shared_from_this and it gives me the same result.
Am Wednesday 28 October 2009 12:43:08 schrieb Jacob Carlborg:
I'm having some problems with bad_weak_ptr exceptions. I have reduced my code into this test case:
#include
#include using boost::shared_ptr; using boost::weak_ptr;
class A { weak_ptr<A> weakThis;
public: virtual ~A(){}
shared_ptr<A> a;
shared_ptr<A> getThis () { shared_ptr<A> x(weakThis); return x; }
void init () { a = getThis(); }
static shared_ptr<A> create () { shared_ptr<A> x(new A);
x->weakThis = x; x->init();
return x; } };
class B : public A { weak_ptr<B> weakThis; public: virtual ~B(){}
shared_ptr<B> getThis () { shared_ptr<B> b(weakThis); return b; }
static shared_ptr<B> create () { shared_ptr<B> x(new B); x->weakThis = x; x->init();
return x; } };
int main (char** argv, int argc) { shared_ptr<B> b = B::create();
return 0; }
If I create an instance of A instead of B or doesn't call x->init() in the B's create function I don't get an exception. The problem is creating a shared_ptr from the weak_ptr in getThis.
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users

Jacob Carlborg wrote:
On 10/28/09 15:56, Stefan Strasser wrote:
I can't make sense of your code ("weakThis" in both A and B, even though B is derived from A?), but have a look at boost::shared_from_this
If I don't have a weak_ptr in B I get this error when trying to create a shared_ptr<B> from a weak_ptr<A>: /shared_ptr.hpp:207: error: invalid conversion from 'A* const' to 'B*'
The compiler is stopping you from doing something unsafe.
I've already tried boost::shared_from_this and it gives me the same result.
I think it would be useful to show what you're actually trying to achieve becasue you can probably do it with shared_from_this(), and if you can't then it looks like it's because of casting issues rather than the shared_from_this code... n
participants (4)
-
Igor R
-
Jacob Carlborg
-
Nigel Rantor
-
Stefan Strasser