terminate called after throwing an instance of ‘std::bad_weak_ptr

在使用shared_ptr时跑到报错terminate called after throwing an instance of 'std::bad_weak_ptr

 

我的类名是这样的class CRtpInstance :public std::enable_shared_from_this<CRtpInstance>,记得使用enable_shared_from_this要用public继承,但这不是报错的原因

 

是因为使用shared_from_this()时,this指针不是shared_ptr,应该先保证自己是shared_ptr才调用

 

例子:

shared_ptr<CRtpInstance >  temp = make_shared<CRtpInstance >();

funExample(shared_from_this());


版权声明:本文为lishun1422840684原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。