tr1::bad_weak_ptr [duplicate]
Possible Duplicate:
enable_shared_from_this - empty internal weak pointer?
AuthConnection::AuthConnection(boost::asio::io_service& io_service)
:Connection(io_service)
{
boost::shared_ptr<AuthHandler>h (new AuthHandler( shared_from_this() ));
this->handler=h;
}
shared_from_this() should return a ptr of Connection , but it's throwing this exception
tr1::bad_weak_ptr
i dont have a clue what's wrong here!
I'm guessing your usage of shared_from_this()
is wrong.
It should be used in a class as follows:
class Y: public enable_shared_from_this<Y>
{
public:
shared_ptr<Y> f()
{
return shared_from_this();
}
}
Then you can call y->f()
to get the this pointer of the class.
It's more then merely this, the actual issue is explained in this question and answer. It has to do with the fact that you can't call shared_from_this()
in the ctor of the derived object.
精彩评论