升压shared_from_this<>()
有人可以用几句话概括一下应该如何使用 boost shared_from_this<>() 智能指针,特别是从使用绑定函数在 io_service 中注册处理程序的角度来看。
编辑:一些回复要求提供更多背景信息。基本上,我正在寻找“陷阱”,即人们使用这种机制观察到的反直觉行为。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我遇到的最大的“问题”是从构造函数中调用shared_from_this是非法的。这直接遵循以下规则:在调用shared_from_this之前,对象的shared_ptr必须存在。
The biggest "gotcha" I've run into is that it's illegal to call shared_from_this from the constructor. This follows directly from the rule that a shared_ptr to the object must exist before you can call shared_from_this.
根据我的理解,有时在您的代码中,您希望一个类向其自身提供
shared_ptr
,以便代码的其他部分可以在构造类的对象后获取该类的对象的 shared_ptr 。问题是,如果你的类只有一个
shared_ptr<>
作为成员变量,它永远不会被自动破坏,因为总是有“最后一个引用”挂在自己身上。继承enable_shared_from_this
为您的类提供了一个自动方法,该方法不仅返回一个shared_ptr
,而且仅将弱共享指针作为成员变量保存,以免影响引用计数。这样,当对它的最后一个引用消失时,您的类将像往常一样被释放。我从未使用过它,但这是我对其工作原理的理解。
From my understanding, sometimes in your code you want a class to offer up
shared_ptr
's to itself so that other parts of your code can obtain shared_ptr's to an object of your class after it has been constructed.The problem is that if your class just has a
shared_ptr<>
to itself as a member variable, it will never get automatically destructed, since there is always "one last reference" hanging around to itself. Inheriting fromenable_shared_from_this
gives your class an automatic method which not only returns ashared_ptr
, but only holds a weak shared pointer as a member variable so as not to affect the reference count. This way, your class will be freed as usual when the last reference to it is gone.I've never used it, but this is my understanding of how it works.
如果对象想要访问指向其自身的
shared_ptr
,则使用shared_from_this<>
。通常,对象只知道隐式
this
指针,而不知道任何管理它的shared_ptr
。此外,this
无法轻松转换为与其他现有shared_ptr
实例共享所有权的shared_ptr
,因此没有对象获取其自身的有效shared_ptr
的简单方法。shared_from_this<>
可以用来解决这个问题。例如:shared_from_this<>
is used if an object wants to get access to ashared_ptr<>
pointing to itself.Usually an object only knows about the implicit
this
pointer, but not about anyshared_ptr<>
managing it. Also,this
cannot easily be converted into ashared_ptr<>
that shares ownership with other existingshared_ptr<>
instances, so there is no easy way for an object to get a validshared_ptr<>
to itself.shared_from_this<>
can be used to solve this problem. For example:boost::asio::io_service
析构函数文档解释得相当好通常,您的对象将链接异步操作,其中处理程序使用
boost::bind
和boost::shared_from_this()
绑定到成员函数。有一些示例使用这个概念。the
boost::asio::io_service
destructor documentation explains it fairly wellTypically your objects will chain asynchronous operations where the handlers are bound to member functions using
boost::bind
andboost::shared_from_this()
. There are some examples that use this concept.上面的一些评论缺少一些内容。这是一个对我有帮助的示例:
Boost enable_shared_from_this 示例
对我来说,我一直在与坏弱指针的错误作斗争。您必须以shared_ptr方式分配您的对象:
并像这样分配一个:
然后您可以执行以下操作:
很多示例向您展示如何使用shared_from_this(),如下所示:
但我缺少的是使用shared_ptr来分配开始的对象。
Stuff is missing from some of the comments above. Here's an example that helped me:
Boost enable_shared_from_this example
For me, I was struggling with errors about bad weak pointers. You HAVE to allocate your object in a shared_ptr fashion:
And allocate one like this:
Then you can do things like:
Lots of examples show you how to use shared_from_this() something like this:
But was missing for me was using a shared_ptr to allocate the object to begin with.