gpt4 book ai didi

c++ - boost::asio -- asio_handler_deallocate 在 io_service::~io_service() 中调用,在 io_service::stop() 之后

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:35:44 26 4
gpt4 key购买 nike

我有一个 ip::udp::socketio_service 构建.只有一个 boost::thread调用 io_service::run()方法,以及 io_service::work 的一个实例防止io_service::run()从返回。我的 ip::udp::socket 的完成处理程序有定制asio_handler_allocate()asio_handler_deallocate()函数,由 my::custom_memory_pool 支持.

当我的应用程序退出时,这一系列事件发生在我的关闭线程上:

  1. ip::udp::socket::close()
  2. work::~work()
  3. io_service::stop()
  4. thread::join()
  5. my::custom_memory_pool::~custom_memory_pool()
  6. ip::udp::socket::~socket()
  7. thread::~thread()
  8. io_service::~io_service()

在第 8 步中,调用 io_service::~io_service()原因...

Program terminated with signal 11, Segmentation fault.
#0 0x00000000005ad93c in my::custom_memory_pool<boost::aligned_storage<512u, -1u> >::deallocate (this=0x36323f8, t=0x7fca97a07880)
at memory.hpp:82
82 reinterpret_cast<pool_node*>(t)->next_ = head_;
(gdb) bt 30
#0 0x00000000005ad93c in my::custom_memory_pool<boost::aligned_storage<512u, -1u> >::deallocate (this=0x36323f8, t=0x7fca97a07880)
at memory.hpp:82
#1 0x00000000005ad40a in asio_handler_deallocate (p=0x7fca97a07880, s=96, h=0x7fffe09d5480) at net.cpp:22
#2 0x0000000000571a07 in boost_asio_handler_alloc_helpers::deallocate<socket_multicast::completion_handler> (p=0x7fca97a07880, s=96, h=...)
at /usr/include/boost/asio/detail/handler_alloc_helpers.hpp:51
#3 0x0000000000558256 in boost::asio::detail::reactive_socket_recvfrom_op<boost::asio::mutable_buffers_1, boost::asio::ip::basic_endpoint<boost::asio::ip::udp>, socket_multicast::completion_handler>::ptr::reset (this=0x7fffe09d54b0)
at /usr/include/boost/asio/detail/reactive_socket_recvfrom_op.hpp:81
#4 0x0000000000558310 in boost::asio::detail::reactive_socket_recvfrom_op<boost::asio::mutable_buffers_1, boost::asio::ip::basic_endpoint<boost::asio::ip::udp>, socket_multicast::completion_handler>::do_complete (owner=0x0, base=0x7fca97a07880)
at /usr/include/boost/asio/detail/reactive_socket_recvfrom_op.hpp:112
#5 0x0000000000426706 in boost::asio::detail::task_io_service_operation::destroy (this=0x7fca97a07880)
at /usr/include/boost/asio/detail/task_io_service_operation.hpp:41
#6 0x000000000042841b in boost::asio::detail::task_io_service::shutdown_service (this=0xd4df30)
at /usr/include/boost/asio/detail/impl/task_io_service.ipp:96
#7 0x0000000000426388 in boost::asio::detail::service_registry::~service_registry (this=0xd4a320, __in_chrg=<value optimized out>)
at /usr/include/boost/asio/detail/impl/service_registry.ipp:43
#8 0x0000000000428e99 in boost::asio::io_service::~io_service (this=0xd49f38, __in_chrg=<value optimized out>)
at /usr/include/boost/asio/impl/io_service.ipp:51

所以 io_service::~io_service()正在尝试将一些内存释放到我在第 5 步中销毁的池中。

我不能动my::custom_memory_pool::~custom_memory_pool()io_service::~io_service()之后.

我预计在 io_service::stop() 之后和 thread::join()返回,不可能再有asio_handler_deallocate()电话。显然不是这样。我可以在第 3 步中做什么来强制 io_service使其所有完成事件出列并释放其所有处理程序内存,以及我如何阻塞直到 io_service完成那些任务?

最佳答案

答案是:当拆除 io_service 及其服务时,根本不要调用 io_service::stop()。只是 work::~work()

io_service::stop() 只是为了暂时暂停io_service,以便稍后io_service::reset() . io_service 的普通正常关闭不应涉及 io_service::stop()

关于c++ - boost::asio -- asio_handler_deallocate 在 io_service::~io_service() 中调用,在 io_service::stop() 之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8013726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com