enable_shared_from_this模板类使用完全解析

本文详细解析了enable_shared_from_this模板类的使用场景及其实现原理,并提供了使用时需要注意的常见陷阱。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自:http://hahaya.github.io/use-enable-shared-from-this/

enable_shared_from_this模板类使用完全解析

以前都没有用过enable_shared_from_this模板类,虽然经常遇到但是也没怎么去关注,今天抽时间好好学习了下enable_shared_from_this模板类,发现在使用shared_ptr模板类和enable_shared_from_this模板类时有许多陷阱的,故记录于此。

在看下面的例子之前,简单说下使用背景,单有一个类,某个函数需要返回当前对象的指针,我们返回的是shared_ptr,为什么使用智能指针呢,这是因为:当我们使用智能指针管理资源时,必须统一使用智能指针,而不能再某些地方使用智能指针,某些地方使用原始指针,否则不能保持智能指针的语义,从而产生各种错误。好了,介绍完背景,看下面的一段小程序:

  
  1. #include <iostream>
  2. #include <boost/shared_ptr.hpp>
  3. class Test
  4. {
  5. public:
  6. //析构函数
  7. ~Test() { std::cout << "Test Destructor." << std::endl; }
  8. //获取指向当前对象的指针
  9. boost::shared_ptr<Test> GetObject()
  10. {
  11. boost::shared_ptr<Test> pTest(this);
  12. return pTest;
  13. }
  14. };
  15. int main(int argc, char *argv[])
  16. {
  17. {
  18. boost::shared_ptr<Test> p( new Test( ));
  19. boost::shared_ptr<Test> q = p->GetObject();
  20. }
  21. return 0;
  22. }

程序输出:

 
  1. Test Destructor.
  2. Test Destructor.

从上面的输出你发现了什么,很明显的发现只创建new了一个Test对象,但是却调用了两次析构函数,这对程序来说肯定是一个灾难。为什么会出现这种情况呢?main函数中的boost::shared_ptr<Test> p( new Test( ));将shared_ptr中引用计数器的值设置为1,而在GetObject函数中又通过boost::shared_ptr<Test> pTest(this);又将shared_ptr中的引用计数器的值增加了1,故在析构时一个Test对象被析构了两次。即产生这个错误的原因是通过同一个Test指针对象创建了多个shared_ptr,这是绝对禁止的。同时这也提醒我们在使用shared_ptr时一定不能通过同一个指针对象创建一个以上的shared_ptr对象。那么有什么方法从一个类的成员函数中获取当前对象的shared_ptr呢,其实方法很简单:只需要该类继承至enable_shared_from_this模板类,然后在需要shared_prt的地方调用enable_shared_from_this模板类的成员函数shared_from_this()即可,下面是改进后的代码:

  
  1. #include <iostream>
  2. #include <boost/enable_shared_from_this.hpp>
  3. #include <boost/shared_ptr.hpp>
  4. class Test : public boost::enable_shared_from_this<Test> //改进1
  5. {
  6. public:
  7. //析构函数
  8. ~Test() { std::cout << "Test Destructor." << std::endl; }
  9. //获取指向当前对象的指针
  10. boost::shared_ptr<Test> GetObject()
  11. {
  12. return shared_from_this(); //改进2
  13. }
  14. };
  15. int main(int argc, char *argv[])
  16. {
  17. {
  18. boost::shared_ptr<Test> p( new Test( ));
  19. boost::shared_ptr<Test> q = p->GetObject();
  20. }
  21. return 0;
  22. }

程序输出:

 
  1. Test Destructor.

从输出对象只被析构了一次,这是我们想要的结果,因此enable_shared_from_this模板类的作用是:用来作为一个基类,它允许从一个成员函数中获得一个当前对象的shared_ptr。那么enable_shared_from_this模板类到底是如何工作的了?请看下文分解~

打开enable_shared_from_this.hpp文件,会发现enable_shared_from_this模板类的实现如下:

  
  1. template<class T> class enable_shared_from_this
  2. {
  3. protected:
  4. enable_shared_from_this() BOOST_NOEXCEPT
  5. {
  6. }
  7. enable_shared_from_this(enable_shared_from_this const &) BOOST_NOEXCEPT
  8. {
  9. }
  10. enable_shared_from_this & operator=(enable_shared_from_this const &) BOOST_NOEXCEPT
  11. {
  12. return *this;
  13. }
  14. ~enable_shared_from_this() BOOST_NOEXCEPT // ~weak_ptr<T> newer throws, so this call also must not throw
  15. {
  16. }
  17. public:
  18. shared_ptr<T> shared_from_this()
  19. {
  20. shared_ptr<T> p( weak_this_ );
  21. BOOST_ASSERT( p.get() == this );
  22. return p;
  23. }
  24. shared_ptr<T const> shared_from_this() const
  25. {
  26. shared_ptr<T const> p( weak_this_ );
  27. BOOST_ASSERT( p.get() == this );
  28. return p;
  29. }
  30. public: // actually private, but avoids compiler template friendship issues
  31. // Note: invoked automatically by shared_ptr; do not call
  32. template<class X, class Y> void _internal_accept_owner( shared_ptr<X> const * ppx, Y * py ) const
  33. {
  34. if( weak_this_.expired() )
  35. {
  36. weak_this_ = shared_ptr<T>( *ppx, py );
  37. }
  38. }
  39. private:
  40. mutable weak_ptr<T> weak_this_;
  41. };

从enable_shared_from_this模板类的实现文件中我们可以很容易的发现我们只能使用返回shared_ptr的shared_from_this()和返回shared_ptr的shared_from_this(),因为这两个版本的shared_from_this()是public权限的,还有一个public权限的是internal_accept_owner函数,但是注释中已经明显指出不能调用这个函数,这个函数会被shared_ptr自动调用,internal_accept_owner函数用来初始化enable_shared_from_this模板类中的唯一成员变量weak_ptr weak_this。而shared_from_this()中是通过将weak_ptr weak_this转化成shared_ptr和shared_ptr来返回的,因此在使用shared_from_this()之前需要先初始化weak_ptr weak_this对象,而weak_ptr weak_this对象是在_internal_accept_owner函数中进行的初始化,也就是说先需要创建shared_ptr对象。即在使用shared_from_this()函数之前,应该先初始化对象的基类enable_shared_from_this,接着再初始化对象,最后初始化shared_ptr。正因为有这个特点所以会出现以下常见的错误:

先来看情形1:

  
  1. class Test : public boost::enable_shared_from_this<Test>
  2. {
  3. Test() { boost::shared_ptr<Test> pTest = shared_from_this(); }
  4. };

这种用法明显是错的,虽然对象的基类enable_shared_from_this类的构造函数已经被调用,但是shared_ptr的构造函数并没有被调用,因此weak_ptr weak_this_并没有被初始化,所以这时调用shared_from_this()是错误的。

接着我们来看情形2:

  
  1. class Test : public boost::enable_shared_from_this<Test>
  2. {
  3. void func() { boost::shared_ptr<Test> pTest = shared_from_this(); }
  4. };
  5. int main()
  6. {
  7. Test test;
  8. test.func(); //错误
  9. Test pTest = new Test;
  10. pTest->func(); //错误
  11. }

同样这种做法也是错误的,和情形1同样的原因shared_ptr的构造函数并没有被调用,因此weak_ptr weak_this_并没有被初始化。

正确的做法应该是:

  
  1. class Test : public boost::enable_shared_from_this<Test>
  2. {
  3. void func() { boost::shared_ptr<Test> pTest = shared_from_this(); }
  4. };
  5. int main()
  6. {
  7. shared_ptr<Test> pTest( new Test() );
  8. pTest->func();
  9. }

shared_ptr<Test> pTest( new Test() );这句话依次执行的顺序是:1 调用enable_shared_from_this的构造函数。2 调用Test的构造函数。 3 调用shared_ptr的构造函数初始化weak_ptr weak_this_。最后才能通过func()函数使用shared_from_this函数。

从上面的错误中我们知道在使用enable_shared_from_this类中的shared_from_this()函数时应该注意:
1. 不能在对象的构造函数中使用shared_from_this()函数。
2. 先需要调用enable_shared_from_this类的构造函数,接着调用对象的构造函数,最后需要调用shared_ptr类的构造函数初始化enable_shared_from_this的成员变量weak_this_。然后才能使用shared_from_this()函数。  
3. 如何程序中使用了智能指针shared_ptr,则程序中统一使用智能指针,不能使用原始指针,以免出现错误。


<think>我们正在讨论C++中的`std::enable_shared_from_this`模板类。根据用户的要求,我们需要介绍其作用、原理和使用方法。同时,我们需要遵循系统级指令,包括使用正确的LaTeX格式、中文回答、生成相关问题,并在引用末尾添加引用标识。 根据提供的引用内容: -引用[1]提到:使用`enable_shared_from_this`必须建立在对象本身被`shared_ptr`管理的前提下,否则会抛出`std::bad_weak_ptr`异常(从C++17开始)。其实现原理与`weak_ptr`密切相关。-引用[2]提到:`enable_shared_from_this`允许对象通过`shared_from_this()`成员函数获得指向自身的`shared_ptr`,用于解决多线程中的悬空指针问题。-引用[3]提到:`enable_shared_from_this`的主要设计目的是解决在类的成员函数中安全地创建`std::shared_ptr`实例的问题,避免两个常见问题(具体问题将在回答中说明)。 因此,我们的回答将围绕这些点展开。 ###回答结构:1.**作用**:解释为什么需要`enable_shared_from_this`,它解决了什么问题。2.**原理**:简要说明其内部实现机制(涉及`weak_ptr`)。 3. **使用方法**:展示如何正确使用这个模板类,并强调注意事项(特别是对象必须由`shared_ptr`管理)。4.**注意事项**:包括异常情况和错误用法。同时,我们将按照要求生成相关问题。###注意事项: -使用`$...$`表示行内数学表达式,但在这个主题中可能数学表达式较少,主要涉及代码和概念。 -独立公式使用`$$...$$`,但同样可能较少。-使用中文回答。 下面开始正式回答。</think>### C++ `enable_shared_from_this` 原理与使用详解 #### **作用与核心问题** `std::enable_shared_from_this` 是 C++ 标准库提供的模板类,用于解决**在对象内部安全获取指向自身的 `shared_ptr`** 的问题。主要避免以下两种错误场景: 1. **多个独立 `shared_ptr` 管理同一对象** 若在类成员函数中直接通过 `this` 创建新 `shared_ptr`(如 `shared_ptr<T>(this)`),会导致多个独立控制块存在,引发重复析构(双释放): ```cpp class Widget { public: void error() { auto sp = std::shared_ptr<Widget>(this); // 危险!独立控制块 } }; ``` 2. **返回裸指针导致悬空引用** 若返回 `this` 裸指针,外部可能将其转换为 `shared_ptr`,同样引发控制块冲突[^3]。 #### **实现原理** `enable_shared_from_this` 的核心依赖 `weak_ptr` 实现: 1. **内部维护 `weak_ptr`** 类继承 `enable_shared_from_this` 后,内部会隐式包含一个 `std::weak_ptr` 成员(通常命名为 `weak_this`)。 2. **构造时初始化 `weak_ptr`** 当对象被 `shared_ptr` 管理时,`shared_ptr` 构造函数会检测基类是否为 `enable_shared_from_this`。若是,则用当前 `shared_ptr` 初始化内部的 `weak_this`。 $$ \text{shared\_ptr<T>::shared\_ptr(T* ptr)} \rightarrow \text{若 T 继承 enable\_shared\_from\_this,则 weak\_this = *this} $$ 3. **`shared_from_this()` 工作流程** ```cpp shared_ptr<T> shared_from_this() { return weak_this.lock(); // 转为 shared_ptr(引用计数增加) } ``` 此操作要求 `weak_this` 已初始化,否则抛出 `std::bad_weak_ptr`[^1]。 #### **正确使用方法** 1. **继承模板类** ```cpp class MyClass : public std::enable_shared_from_this<MyClass> { public: std::shared_ptr<MyClass> get_ptr() { return shared_from_this(); // 安全获取 } }; ``` 2. **对象必须由 `shared_ptr` 管理** ```cpp // 正确用法 auto obj = std::make_shared<MyClass>(); auto sp = obj->get_ptr(); // 引用计数变为 2 // 错误用法:未通过 shared_ptr 构造 MyClass raw_obj; raw_obj.get_ptr(); // 抛出 std::bad_weak_ptr ``` 3. **禁止在构造函数中调用** 构造函数执行时 `weak_this` 尚未初始化,调用 `shared_from_this()` 会导致未定义行为。 #### **典型应用场景** 1. **异步回调** 在多线程中传递自身智能指针,确保对象生命周期覆盖回调执行: ```cpp class Session : public enable_shared_from_this<Session> { void async_send() { auto self = shared_from_this(); io_service.post([self] { self->handle_send(); }); // 避免回调时对象被销毁 } }; ``` 2. **链式调用** ```cpp class Parser : public enable_shared_from_this<Parser> { shared_ptr<Parser> set_option(Option opt) { options_ = opt; return shared_from_this(); // 返回自身指针用于链式调用 } }; ``` #### **注意事项** - **前置条件**:对象必须已被 `shared_ptr` 管理,否则 `shared_from_this()` 抛异常(C++17 起)[^1]。 - **性能开销**:内部 `weak_ptr` 带来少量内存和原子操作开销。 - **替代方案**:C++23 引入 `std::make_shared_from_this` 简化构造。 > 关键总结:`enable_shared_from_this` 通过 `weak_ptr` 桥接 `shared_ptr` 的控制块,实现安全共享自身所有权,是异步编程和资源管理的核心工具[^2][^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值