英文:
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<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();
}
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论