program crash with boost::asio::spawn and socket::async_receive_from

家住魔仙堡 提交于 2019-12-02 08:20:43

For starters, your code cannot possibly compile (handler must be a non-static member function of Worker, but that means it doesn't satisfy the handler requirements for async_receive_from.

But the problem you ask about appears simpler:

 auto ob1 = std::make_shared<Worker>(io_service);

Creates a shared pointer

 boost::asio::spawn(*io_service, [ob1](
                                  boost::asio::yield_context yield) {

Posts a coro onto the service, that holds a copy of ob1 so it stays alive. So far so good.

  ob1->AsyncRead();

This does an async_recieve_from, which naturally returns immediately, the coro is done and ob1 is released. But async_receive_from is still pending on the service.

If you want to use async operations inside a coro, you have to pass the yield_context as a completion token. You didn't do that.

Your code should conceptually look like:

  void work(boost::asio::io_service* io) {

  boost::asio::spawn(*io_service, [io](boost::asio::yield_context yield) {

       udp::socket socket(*io); 
       // ... more

       udp::endpoint ep;
       char buffer[1024];
       socket.async_receive_from(buffer, ep, yield); // throws on error

   });
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!