繁体   English   中英

在抛出 'std::bad_weak_ptr' what(): bad_weak_ptr 的实例后调用终止?

[英]terminate called after throwing an instance of 'std::bad_weak_ptr' what(): bad_weak_ptr?

我正在学习智能指针和shared_from_this 在类继承关系中,将很难理解。

我有两个基类CACB ,它们派生自enable_shared_from_this ,子类CC派生自CACB 我想从类 self 中取出三个类的共享指针,所以我写了sharedCAfromThissharedCBfromThissharedCCfromthis

class CA  : private enable_shared_from_this<CA> {
public:
    shared_ptr<CA> sharedCAfromThis() { return shared_from_this();  }
    virtual ~CA() {};
    void print() {
        cout << "CA" << endl;
    }
};

class CB : private enable_shared_from_this<CB> {
public:
    shared_ptr<CB> sharedCBfromThis() { return shared_from_this();  }
    virtual ~CB() {};
    void print() {
        cout << "CB" << endl;
    }
};

class CC : public CA, CB {
public:
    shared_ptr<CC> sharedCCfromThis() { return dynamic_pointer_cast<CC>(sharedCAfromThis());  }
    virtual ~CC() {};
    void print() {
        cout << "CC" << endl;
    }
};

int main()
{
    shared_ptr<CC> cc_ptr = make_shared<CC>();
    cc_ptr->sharedCAfromThis()->print();
    //shared_ptr<C> c_ptr = make_shared<C>();
    //c_ptr->get_shared_b()->printb();
    while (1);
}

但我错了,问题是:

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
terminate called after throwing an instance of 'std::bad_weak_ptr'
  what():  bad_weak_ptr

为什么我会收到此错误消息?

你好,是的,非常感谢,我把private改成public了,但是问题依然存在。 我的 gcc 版本是 8.0; 我将代码更改如下。

class CA  : public enable_shared_from_this<CA> {
public:
    shared_ptr<CA> sharedCAfromThis() { return shared_from_this();  }
    virtual ~CA() {};
    void print() {
        cout << "CA" << endl;
    }
};

class CB : public enable_shared_from_this<CB> {
public:
    shared_ptr<CB> sharedCBfromThis() { return shared_from_this();  }
    virtual ~CB() {};
    void print() {
        cout << "CB" << endl;
    }
};

class CC : public CA, CB, enable_shared_from_this<CC> {
public:
    shared_ptr<CC> sharedCCfromThis() { return dynamic_pointer_cast<CC>(sharedCAfromThis());  }
    virtual ~CC() {};
    void print() {
        cout << "CC" << endl;
    }
};

您应该从enable_shared_from_this公开继承。 [util.smartptr.shared.const]/1

在下面的构造函数定义中,对于Y*类型的指针p ,启用shared_from_thisp意味着如果Y具有明确且可访问的基类,该基类enable_shared_from_this ,则remove_cv_t<Y>*应隐式转换为T*并且构造函数评估语句:

 if (p != nullptr && p->weak_this.expired()) p->weak_this = shared_ptr<remove_cv_t<Y>>(*this, const_cast<remove_cv_t<Y>*>(p));

weak_this成员的分配不是原子的,并且与对同一对象([intro.multithread])的任何潜在并发访问相冲突。

如果使用私有继承,则无法再访问基类。

看起来你的问题是你有一个重复。 您需要确保只有一个enable_shared_from_this

要使用类做到这一点,您可以虚拟地派生该类:

class A : virtual public enable_shared_from_this<A> ...
class B : virtual public enable_shared_from_this<B> ...

class C : public A, public B ...

现在您有一个enable_shared_from_this<>实例,这应该可以正常工作。 否则,它可能使用来自 B 的版本,这可能是一个nullptr ,因此会出现错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM