开发者

tr1::bad_weak_ptr [duplicate]

This question already has answers here: 开发者_开发百科 Closed 11 years ago.

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜