Boost UDP套接字上的SIGSEGV关闭-malloc.c上的tcache_get

时间:2018-10-10 10:48:33

标签: c++ sockets boost-asio

我不知道此问题可能来自哪里: (使用GDB进行调试):

terminate called after throwing an instance of '[Thread 0x7ffff5e68700 (LWP 24945) exited]
boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'

线程1“ random_walkerta”收到信号SIGSEGV,分段错误。 talloc_get(tc_idx = 1)在malloc.c:2943

StackTrace:

#0  tcache_get (tc_idx=1) at malloc.c:2943
#1  __GI___libc_malloc (bytes=31) at malloc.c:3050
#2  0x00007ffff6ec154c in operator new(unsigned long) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff6f56dbf in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_mutate(unsigned long, unsigned long, char const*, unsigned long) () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff6f584bb in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_append(char const*, unsigned long) ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00005555555bee66 in boost::system::system_error::what (
    this=0x5555558a1bf0) at /usr/include/boost/system/system_error.hpp:70
#6  0x00007ffff6eba8ba in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#7  0x00007ffff6ec0d3a in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#8  0x00007ffff6ebfd59 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#9  0x00007ffff6ec0708 in __gxx_personality_v0 ()
   from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#10 0x00007ffff6888763 in ?? () from /lib/x86_64-linux-gnu/libgcc_s.so.1
#11 0x00007ffff688907d in _Unwind_Resume ()
   from /lib/x86_64-linux-gnu/libgcc_s.so.1
#12 0x00005555555cb8d6 in boost::asio::detail::do_throw_error (err=..., 
    location=0x555555610dba "close")
    at /usr/include/boost/asio/detail/impl/throw_error.ipp:37
---Type <return> to continue, or q <return> to quit---
#13 0x00005555555cb741 in boost::asio::detail::throw_error (err=..., 
    location=0x555555610dba "close")
    at /usr/include/boost/asio/detail/throw_error.hpp:42
#14 0x00005555555de022 in boost::asio::basic_socket<boost::asio::ip::udp, boost::asio::datagram_socket_service<boost::asio::ip::udp> >::close (
    this=0x555555887ea0) at /usr/include/boost/asio/basic_socket.hpp:356
#15 0x00005555555d7ebe in Vast::net_overhearing_handler::handle_close (
    this=0x555555889490) at net_overhearing_handler.cpp:160
#16 0x00005555555d7e4c in Vast::net_overhearing_handler::close (
    this=0x555555889490) at net_overhearing_handler.cpp:141
#17 0x00005555555c9b85 in Vast::net_overhearing::stop (this=0x5555558870e0)
    at net_overhearing.cpp:88
#18 0x00005555555ba77f in Vast::VASTnet::~VASTnet (this=0x55555587f180, 
    __in_chrg=<optimized out>) at VASTnet.cpp:63
#19 0x000055555556ca7a in Vast::destroyNet (net=0x55555587f180)
    at VASTVerse.cpp:93
#20 0x000055555556d036 in Vast::VASTVerse::~VASTVerse (this=0x55555587e4c0, 
    __in_chrg=<optimized out>) at VASTVerse.cpp:196
#21 0x000055555556bce4 in main (argc=1, argv=0x7fffffffe598)
    at random_walkertalker.cpp:266

设置如下: 我在单独的线程io_service上运行一个Boost UDP套接字,监听数据包。一切都会按预期进行,直到我尝试关闭程序,UDP套接字和io_service。我猜我没有正确关闭某些设备。

这是关闭UDP和io_services的代码:

if (_io_service != NULL) {
   _io_service->reset();
   _udp->close();                 //This line is giving the error (160)  


   _io_service->stop();
   _iosthread->join();
}

启动时,我将执行以下操作:

_udp = new ip::udp::socket(*_io_service);
_udp->open(ip::udp::v4());

_udp->async_receive_from(
  boost::asio::buffer(_buf, VAST_BUFSIZ), _remote_endpoint_,
  boost::bind(&net_overhearing_handler::handle_input, this,
  boost::asio::placeholders::error,
  boost::asio::placeholders::bytes_transferred));

_iosthread = new boost::thread(boost::bind(&boost::asio::io_service::run, io_service));

1 个答案:

答案 0 :(得分:0)

删除此行

 _io_service->reset();

因为根据参考文献io_service::reset

  

有未完成的呼叫时,不得调用此功能   到run(),run_one(),poll()或poll_one()函数。

当您调用run时,似乎restart方法在线程中起作用。

您无需调用此方法即可停止io_service::run方法。通过调用run取消_io_service->stop();中所有待处理的操作,然后run停止,您的线程也终止。