使用Boost ASIO通道时出现分段错误。

huangapple go评论58阅读模式
英文:

Segfault when using Boost ASIO channels

问题

我正在尝试使用Boost::ASIO中的通道实现协程取消,但是我遇到了段错误并且无法找出原因。
我尝试使用调试器,但它没有提供太多信息,只是在"io_ctx->run()"之后发生段错误。

以下是我的代码:

boost::asio::awaitable<void> to_be_cancelled(
    boost::asio::experimental::concurrent_channel<void(boost::system::error_code, std::string)>& end,
    shared_ptr<boost::asio::io_context> io_ctx)
{
    boost::asio::ip::tcp::socket sock(*io_ctx);
    boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8000);
    boost::asio::ip::tcp::acceptor acceptor(*io_ctx, endpoint);
    co_await
    (
        end.async_receive(boost::asio::use_awaitable_t()) ||
        acceptor.async_accept(sock, boost::asio::use_awaitable_t())
    );

    cout << "done\n";
}

boost::asio::awaitable<void> send_one_message(
    boost::asio::experimental::concurrent_channel<void(boost::system::error_code, std::string)>& channel)
{
    channel.async_send(boost::system::error_code{}, string("hello"), boost::asio::use_awaitable_t());
}

boost::asio::awaitable<void> coroutine_manager(shared_ptr<boost::asio::io_service> io_ctx)
{
    auto ch = make_shared<boost::asio::experimental::concurrent_channel<void(boost::system::error_code, std::string)>>(*io_ctx, 5);
    co_await (to_be_cancelled(*ch, io_ctx) && send_one_message(*ch));
}

int main()
{
    auto io_ctx = std::make_shared<boost::asio::io_service>(2);
    co_spawn(*io_ctx, coroutine_manager(io_ctx), boost::asio::detached);
    io_ctx->run();
}

希望能提供一些帮助。

英文:

I'm trying to implement coroutine cancellation using channels in Boost::ASIO, however I'm getting segmentation faults and I can't figure out the reason.
I tried using a debugger, but it didn't tell me much, just that the segfault happens after "io_ctx->run()".

Here's my code:

boost::asio::awaitable&lt;void&gt; to_be_cancelled(
    boost::asio::experimental::concurrent_channel&lt;void(boost::system::error_code, std::string)&gt;&amp; end,
    shared_ptr&lt;boost::asio::io_context&gt; io_ctx)
{
    boost::asio::ip::tcp::socket sock(*io_ctx);
    boost::asio::ip::tcp::endpoint endpoint(boost::asio::ip::tcp::v4(), 8000);
    boost::asio::ip::tcp::acceptor acceptor(*io_ctx, endpoint);
    co_await
    (
        end.async_receive(boost::asio::use_awaitable_t()) ||
        acceptor.async_accept(sock, boost::asio::use_awaitable_t())
    );

    cout &lt;&lt; &quot;done\n&quot;;
}

boost::asio::awaitable&lt;void&gt; send_one_message(
    boost::asio::experimental::concurrent_channel&lt;void(boost::system::error_code, std::string)&gt;&amp; channel)
{
    channel.async_send(boost::system::error_code{}, string(&quot;hello&quot;), boost::asio::use_awaitable_t());
}

boost::asio::awaitable&lt;void&gt; coroutine_manager(shared_ptr&lt;boost::asio::io_service&gt; io_ctx)
{
    auto ch = make_shared&lt;boost::asio::experimental::concurrent_channel&lt;void(boost::system::error_code, std::string)&gt;&gt;(*io_ctx, 5);
    co_await (to_be_cancelled(*ch, io_ctx) &amp;&amp; send_one_message(*ch));
}

int main()
{
    auto io_ctx = std::make_shared&lt;boost::asio::io_service&gt;(2);
    co_spawn(*io_ctx, coroutine_manager(io_ctx), boost::asio::detached);
    io_ctx-&gt;run();
}

Any help would be much appreciated.

答案1

得分: 1

我在“send_one_message”函数中缺少一个“co_await”,我猜这意味着该字符串将超出作用域,导致接收通道尝试访问已释放的内存。

英文:

I was missing a "co_await" in my "send_one_message" function, which I guess meant the string would go out of scope resulting in the receiving channel trying to access freed memory.

huangapple
  • 本文由 发表于 2023年2月16日 07:48:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/75466489.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定