升压shared_from_this<>()

发布于 2024-09-17 11:15:16 字数 160 浏览 11 评论 0 原文

有人可以用几句话概括一下应该如何使用 boost shared_from_this<>() 智能指针,特别是从使用绑定函数在 io_service 中注册处理程序的角度来看。

编辑:一些回复要求提供更多背景信息。基本上,我正在寻找“陷阱”,即人们使用这种机制观察到的反直觉行为。

could someone summarize in a few succinct words how the boost shared_from_this<>() smart pointer should be used, particularly from the perspective of registering handlers in the io_service using the bind function.

EDIT: Some of the responses have asked for more context. Basically, I'm looking for "gotchas", counter-intuitive behaviour people have observed using this mechanism.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

彼岸花ソ最美的依靠 2024-09-24 11:15:16

我遇到的最大的“问题”是从构造函数中调用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.

缘字诀 2024-09-24 11:15:16

根据我的理解,有时在您的代码中,您希望一个类向其自身提供 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 from enable_shared_from_this gives your class an automatic method which not only returns a shared_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.

淡淡绿茶香 2024-09-24 11:15:16

如果对象想要访问指向其自身的 shared_ptr,则使用 shared_from_this<>

通常,对象只知道隐式 this 指针,而不知道任何管理它的 shared_ptr 。此外,this 无法轻松转换为与其他现有 shared_ptr 实例共享所有权的 shared_ptr,因此没有对象获取其自身的有效 shared_ptr 的简单方法。

shared_from_this<>可以用来解决这个问题。例如:

struct A : boost::enable_shared_from_this<A> {
   server *io;
   // ...

   void register_self() {
      io->add_client(shared_from_this());
   }
};

shared_from_this<> is used if an object wants to get access to a shared_ptr<> pointing to itself.

Usually an object only knows about the implicit this pointer, but not about any shared_ptr<> managing it. Also, this cannot easily be converted into a shared_ptr<> that shares ownership with other existing shared_ptr<> instances, so there is no easy way for an object to get a valid shared_ptr<> to itself.

shared_from_this<> can be used to solve this problem. For example:

struct A : boost::enable_shared_from_this<A> {
   server *io;
   // ...

   void register_self() {
      io->add_client(shared_from_this());
   }
};
云淡月浅 2024-09-24 11:15:16

boost::asio::io_service 析构函数文档解释得相当好

描述的破坏顺序
以上允许程序简化
他们的资源管理通过使用
共享_ptr<>。物体所在的地方
生命周期与一个人的生命周期息息相关
连接(或其他一些序列
异步操作),一个shared_ptr
到该对象将被绑定到
所有异步的处理程序
与其相关的操作。这
工作原理如下:

  • 当单个连接结束时,所有关联的异步操作
    完全的。对应的处理程序
    物体被摧毁,所有
    对象的shared_ptr引用
    被摧毁。
  • 要关闭整个程序,需要调用 io_service 函数 stop()
    尽快终止任何 run() 调用
    尽可能。 io_service 析构函数
    上面定义的销毁所有处理程序,
    导致所有shared_ptr引用
    所有连接对象
    被毁了。

通常,您的对象将链接异步操作,其中处理程序使用 boost::bindboost::shared_from_this() 绑定到成员函数。有一些示例使用这个概念。

the boost::asio::io_service destructor documentation explains it fairly well

The destruction sequence described
above permits programs to simplify
their resource management by using
shared_ptr<>. Where an object's
lifetime is tied to the lifetime of a
connection (or some other sequence of
asynchronous operations), a shared_ptr
to the object would be bound into the
handlers for all asynchronous
operations associated with it. This
works as follows:

  • When a single connection ends, all associated asynchronous operations
    complete. The corresponding handler
    objects are destroyed, and all
    shared_ptr references to the objects
    are destroyed.
  • To shut down the whole program, the io_service function stop() is called
    to terminate any run() calls as soon
    as possible. The io_service destructor
    defined above destroys all handlers,
    causing all shared_ptr references to
    all connection objects to be
    destroyed.

Typically your objects will chain asynchronous operations where the handlers are bound to member functions using boost::bind and boost::shared_from_this(). There are some examples that use this concept.

酒几许 2024-09-24 11:15:16

上面的一些评论缺少一些内容。这是一个对我有帮助的示例:

Boost enable_shared_from_this 示例

对我来说,我一直在与坏弱指针的错误作斗争。您必须以shared_ptr方式分配您的对象:

class SyncSocket: public boost::enable_shared_from_this<SyncSocket>

并像这样分配一个:

boost::shared_ptr<SyncSocket> socket(new SyncSocket);

然后您可以执行以下操作:

socket->connect(...);

很多示例向您展示如何使用shared_from_this(),如下所示:

boost::asio::async_read_until(socket, receiveBuffer, haveData,
        boost::bind(&SyncSocket::dataReceived, shared_from_this(), boost::asio::placeholders::error));

但我缺少的是使用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:

class SyncSocket: public boost::enable_shared_from_this<SyncSocket>

And allocate one like this:

boost::shared_ptr<SyncSocket> socket(new SyncSocket);

Then you can do things like:

socket->connect(...);

Lots of examples show you how to use shared_from_this() something like this:

boost::asio::async_read_until(socket, receiveBuffer, haveData,
        boost::bind(&SyncSocket::dataReceived, shared_from_this(), boost::asio::placeholders::error));

But was missing for me was using a shared_ptr to allocate the object to begin with.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文