tr1::bad_weak_ptr

发布于 2024-11-14 14:52:46 字数 576 浏览 5 评论 0原文

可能的重复:
enable_shared_from_this - 空内部弱指针?

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() 应该返回Connection 的 ptr ,但它抛出了这个异常,

tr1::bad_weak_ptr

我不知道这里出了什么问题!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仄言 2024-11-21 14:52:46

我猜您对 shared_from_this() 的使用是错误的。

它应该在类中使用,如下所示:

class Y: public enable_shared_from_this<Y>
{
public:

    shared_ptr<Y> f()
    {
        return shared_from_this();
    }
}

然后您可以调用y->f()来获取该类的this指针。

不仅仅是这个,实际问题在 this 问题与解答。这与您无法在派生对象的构造函数中调用 shared_from_this() 的事实有关。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文