shared_from_this使用中运行错误bad_weak_ptr原因分析

我正在使用独立的Asio和C++ 11创建一个C++服务器应用程序,并且收到错误,这就是我寻求帮助的原因。

1. 现象

在类中调用shared_from_this()时bad_weak_ptr会引发异常,导致程序崩溃。

2. 原因

  • 在构造函数中调用shared_from_this()
  • 调用shared_from_this()的类不是以智能指针的形式初始化对象的。
  • 不是public继承

3. 构造函数中调用shared_from_this()

enable_from_this 的使用与实现原理说明:

shared_from_this()是enable_shared_from_this的成员函数,返回shared_ptr;

注意的是,这个函数仅在shared_ptr的构造函数被调用之后才能使用。

原因是enable_shared_from_this::weak_ptr并不在构造函数中设置,而是在shared_ptr的构造函数中设置。

错误代码示例

#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
 
#include <iostream>
using namespace std;
 
class D: public boost::enable_shared_from_this<D>
{
public:
    D() {
        cout<<"D::D()"<<endl;
        boost::shared_ptr<D> p = shared_from_this();
    }    
};
 
int main() {
    boost::shared_ptr<D> a(new D);
    return 0;    
}

 程序编译通过,执行结果如下:

D::D()
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr
Aborted

说明:在D的构造函数中调用shared_from_this(),此时D的实例本身尚未构造成功,weak_ptr也就尚未设置,所以程序抛出tr1::bad_weak_ptr异常。

4. 调用shared_from_this()的类不是以智能指针的形式初始化对象的

错误代码示例

#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
 
#include <iostream>
using namespace std;
 
class D: public boost::enable_shared_from_this<D>
{
public:
    D() {
        cout<<"D::D()"<<endl;
    }
    
    void func() {
        cout<<"D::func()"<<endl;
        boost::shared_ptr<D> p = shared_from_this();
    }    
};
 
int main() {
    D d;
    d.func();
    return 0;    
}

程序编译通过,执行结果如下:

D::D()
D::func()
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_weak_ptr> >'
what(): tr1::bad_weak_ptr
Aborted

失败原因分析:在主函数main中,D的实例是在栈上构造,没有使用boost::shared_ptr 的构造方式,所以boost::enable_shared_from_this中的weak_ptr所指的函数对象也就没有被赋值,在调用d.func()中使用shared_from_this()函数时

注:shared_from_this的函数实现

shared_ptr<T> shared_from_this()
{
  shared_ptr<T> p( weak_this_ );
  BOOST_ASSERT( p.get() == this );
  return p;
}

调用BOOST_ASSERT( p.get() == this ); 失败,抛出以上异常。

正确使用示例

#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
 
#include <iostream>
using namespace std;
 
class D: public boost::enable_shared_from_this<D>
{
public:
    D() {
        cout<<"D::D()"<<endl;
    }
    
    void func() {
        cout<<"D::func()"<<endl;
        boost::shared_ptr<D> p = shared_from_this();
    }    
};
 
int main() {
    boost::shared_ptr<D> p(new D);
    p->func();
    return 0;    
}    

执行结果

D::D()
D::func()

5. 不是public继承

必须使用public继承,例如

class Overlooked : public std::enable_shared_from_this<Overlooked> {
public:
 void foo() {
   auto self = shared_from_this();
    //...
   }
}; 

否则就会崩溃,例如

class Overlooked : std::enable_shared_from_this<Overlooked> {
public:
 void foo() {
   auto self = shared_from_this(); //std::bad_weak_ptr
    //...
   }
   //...
}; 

参考文献

C++ | enable_shared_from_this - overview, examples, and internals - nextptr

bad_weak_ptr的原因_华秋实的博客-CSDN博客

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值