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

标签 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));

最佳答案

删除此行

 _io_service->reset();

因为根据引用io_service::reset

This function must not be called while there are any unfinished calls to the run(), run_one(), poll() or poll_one() functions.



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

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

关于c++ - Boost UDP套接字上的SIGSEGV关闭-malloc.c上的tcache_get,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52738515/

相关文章:

c++ - 删除指针但不为 NULL

java - 简单的 TCP 客户端-服务器应用程序抛出异常

c - getline() 函数行为异常

python - 服务器/客户端脚本在 MacOSX 上运行流畅,但在 Ubuntu 上运行失败

c++ - 以下 boost::asio 教程在他们的代码中出现错误

c++ - 我如何从 strace 输出中确定我的程序的哪一部分未能获得互斥锁

c++ - 如何在 C++ 中模拟成员对象

c++ - Visual Studio 2012 试图缩进代码块更改代码

c++11 - 为什么在使用异步操作时没有堆栈溢出?

由于设置 `set_verify_mode(boost::asio::ssl::verify_none);` 造成的安全后果