Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用异步写入发送数据时崩溃_C++_Asio - Fatal编程技术网

C++ 使用异步写入发送数据时崩溃

C++ 使用异步写入发送数据时崩溃,c++,asio,C++,Asio,我的服务器已经运行了20多个小时了。在此期间,我不断地对服务器进行压力测试。有时会崩溃,有时不会崩溃。我使用Asio只是为了处理我的网络类 void AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>& packet) { sendding_.store(true); auto self = shared_from_this(); socket_.async_write_some(asio:

我的服务器已经运行了20多个小时了。在此期间,我不断地对服务器进行压力测试。有时会崩溃,有时不会崩溃。我使用Asio只是为了处理我的网络类

void AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>& packet)
{
    sendding_.store(true);
    auto self = shared_from_this();
    socket_.async_write_some(asio::buffer(packet->peek(), packet->readableBytes()),
        [self, packet](const std::error_code& err, std::size_t writen)
        {
            self->AsyncWriteSomeCallback(err,packet,writen);
        });
}

void  AsioChannel::AsyncWriteSomeCallback(const std::error_code& error, const std::shared_ptr<Buffer>& packet, std::size_t writen)
{
    if (error)
    {
    
        return;
    }
 
    if (writen < packet->readableBytes())
    {
        packet->retrieve(writen);
        AsyncSendData(packet);
    }
    else
    {
        if(!waitwriteq_.empty())
        {
            auto next = waitwriteq_.front();
            waitwriteq_.pop();
            AsyncSendData(next);
        }
        else
        {
            sendding_.store(false);
        }
    }
}
void AsioChannel::AsyncSendData(const std::shared_ptr&packet)
{
sendding.store(真);
auto self=shared_from_this();
socket\u.async\u write\u some(asio::buffer(packet->peek(),packet->readableBytes()),
[self,packet](常量std::error\u code&err,std::size\u t writen)
{
self->asyncWriteMeCallback(错误、数据包、写入);
});
}
void AsioChannel::asynchWritesMeCallback(常量std::error\u code&error,常量std::shared\u ptr&packet,std::size\u t writen)
{
如果(错误)
{
返回;
}
if(writenreadableBytes())
{
数据包->检索(写入);
异步发送数据(数据包);
}
其他的
{
如果(!waitwriteq_uuq.empty())
{
自动下一步=waitwriteq_uuq.front();
waitwriteq_uuq.pop();
AsyncSendData(下一步);
}
其他的
{
发送数据存储(假);
}
}
}
崩溃堆栈如下所示

#0  0x00007f172a8255f7 in raise () from /lib64/libc.so.6
#1  0x00007f172a826e28 in abort () from /lib64/libc.so.6
#2  0x00007f172a865317 in __libc_message () from /lib64/libc.so.6
#3  0x00007f172a86cfe1 in _int_free () from /lib64/libc.so.6
#4  0x00007f172b92b49d in allocate<asio::detail::thread_info_base::default_tag> (size=168, this_thread=<optimized out>) at /opt/env/include/c++/10.2.0/ext/atomicity.h:97
#5  allocate (size=168, this_thread=<optimized out>) at asio/detail/thread_info_base.hpp:73
#6  asio_handler_allocate (size=168) at asio/impl/handler_alloc_hook.ipp:34
#7  allocate<net::AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>&)::<lambda(const std::error_code&, std::size_t)> > (h=..., s=168)
    at asio/detail/handler_alloc_helpers.hpp:70
#8  allocate (n=1, this=<synthetic pointer>) at asio/detail/handler_alloc_helpers.hpp:127
#9  allocate (handler=...) at asio/detail/reactive_socket_send_op.hpp:103
#10 async_send<asio::const_buffers_1, net::AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>&)::<lambda(const std::error_code&, std::size_t)>, asio::execution::any_executor<asio::execution::context_as_t<asio::execution_context&>, asio::execution::detail::blocking::never_t<0>, asio::execution::prefer_only<asio::execution::detail::blocking::possibly_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::tracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::outstanding_work::untracked_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::fork_t<0> >, asio::execution::prefer_only<asio::execution::detail::relationship::continuation_t<0> > > > (io_ex=..., handler=..., flags=0,
    buffers=..., impl=..., this=0x7f17080009f8) at asio/detail/reactive_socket_service_base.hpp:281
#11 operator()<net::AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>&)::<lambda(const std::error_code&, std::size_t)>, asio::const_buffers_1> (flags=0, buffers=...,
    handler=<unknown type in /libgamesh.net.so, CU 0x0, DIE 0x6e7c8>, this=<optimized out>)
    at asio/basic_stream_socket.hpp:1005
#12 initiate<asio::basic_stream_socket<asio::ip::tcp>::initiate_async_send, net::AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>&)::<lambda(const std::error_code&, std::size_t)>, const asio::const_buffers_1&, int> (token=<unknown type in /libgamesh.net.so, CU 0x0, DIE 0x6e78e>,
    initiation=<optimized out>) at asio/async_result.hpp:151
#13 async_initiate<net::AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>&)::<lambda(const std::error_code&, std::size_t)>, void(std::error_code, long unsigned int), asio::basic_stream_socket<asio::ip::tcp>::initiate_async_send, const asio::const_buffers_1&, int> (token=..., initiation=<optimized out>)
    at asio/async_result.hpp:364
#14 async_write_some<asio::const_buffers_1, net::AsioChannel::AsyncSendData(const std::shared_ptr<Buffer>&)::<lambda(const std::error_code&, std::size_t)> > (
    handler=<unknown type in /libgamesh.net.so, CU 0x0, DIE 0x6e6e1>, buffers=..., this=0x7f1708143480)
    at asio/basic_stream_socket.hpp:855
#15 net::AsioChannel::AsyncSendData (this=0x7f1708143460, packet=...) at net/asio_channel.cpp:326
#16 0x00007f172b92b964 in net::AsioChannel::SendPacket (this=<optimized out>, packet=..., size=<optimized out>)
    at net/asio_channel.cpp:284
#17 0x00007f172b93cb1e in net::AsioServerHandler::SendPacket (this=<optimized out>, pPacket=..., size=<optimized out>) at /opt/env/include/c++/10.2.0/bits/shared_ptr_base.h:1324
#18 0x00007f172b946831 in operator() (__closure=<optimized out>) at /opt/env/include/c++/10.2.0/bits/shared_ptr_base.h:1324
#19 __invoke_impl<void, net::Asio_SendPacket(uint32_t, char const*, uint32_t)::<lambda()>&> (__f=...) at /opt/env/include/c++/10.2.0/bits/invoke.h:60
#20 __invoke_r<void, net::Asio_SendPacket(uint32_t, char const*, uint32_t)::<lambda()>&> (__fn=...) at /opt/env/include/c++/10.2.0/bits/invoke.h:110
#21 std::_Function_handler<void(), net::Asio_SendPacket(uint32_t, char const*, uint32_t)::<lambda()> >::_M_invoke(const std::_Any_data &) (__functor=...)
    at /opt/env/include/c++/10.2.0/bits/std_function.h:291
#22 0x00007f172c42ee26 in std::function<void ()>::operator()() const (this=<optimized out>) at /opt/env/include/c++/10.2.0/bits/std_function.h:248
#23 0x00007f172b93b0b4 in asio_handler_invoke<std::function<void()> > (function=...)
    at asio/handler_invoke_hook.hpp:85
#24 invoke<std::function<void()>, std::function<void()> > (context=..., function=...)
    at asio/detail/handler_invoke_helpers.hpp:54
#25 complete<std::function<void()> > (handler=..., function=..., this=<synthetic pointer>)
    at asio/detail/handler_work.hpp:424
#26 asio::detail::completion_handler<std::function<void ()>, asio::io_context::basic_executor_type<std::allocator<void>, 0u> >::do_complete(void*, asio::detail::scheduler_operation*, std::error_code const&, unsigned long) (owner=0x5011470, base=0x7f15c7b2f4a0) at asio/detail/completion_handler.hpp:73
#27 0x00007f172b932d88 in complete (bytes_transferred=0, ec=..., owner=0x5011470, this=0x7f15c7b2f4a0)
    at asio/detail/scheduler_operation.hpp:39
#28 asio::detail::scheduler::do_run_one (this=this@entry=0x5011470, lock=..., this_thread=..., ec=...)
    at asio/detail/impl/scheduler.ipp:480
#29 0x00007f172b934336 in asio::detail::scheduler::run (this=0x5011470, ec=...)
    at asio/detail/impl/scheduler.ipp:203
---Type <return> to continue, or q <return> to quit---
#30 0x00007f172b9394c6 in run (this=0x50113c8) at asio/impl/io_context.ipp:62
#31 net::AsioEventLoop::Run (this=0x50113c0) at net/asio_event_loop.cpp:30
#32 0x00007f172b942ae1 in net::EventLoopThread::Run(std::function<int ()> const&, std::function<int ()> const&) (this=0x4fe9ec0, pre=..., post=...)
    at /opt/env/include/c++/10.2.0/bits/unique_ptr.h:421
#33 0x00007f172b942e6f in __invoke_impl<void, void (net::EventLoopThread::*&)(const std::function<int()>&, const std::function<int()>&), net::EventLoopThread*&, std::function<int()>&, std::function<int()>&> (__t=<optimized out>, __f=<optimized out>) at /opt/env/include/c++/10.2.0/bits/invoke.h:89
#34 __invoke<void (net::EventLoopThread::*&)(const std::function<int()>&, const std::function<int()>&), net::EventLoopThread*&, std::function<int()>&, std::function<int()>&> (
    __fn=<optimized out>) at /opt/env/include/c++/10.2.0/bits/invoke.h:95
#35 __call<void, 0, 1, 2> (__args=<optimized out>, this=<optimized out>) at /opt/env/include/c++/10.2.0/functional:416
#36 operator()<> (this=<optimized out>) at /opt/env/include/c++/10.2.0/functional:499
#37 __invoke_impl<void, std::_Bind<void (net::EventLoopThread::*(net::EventLoopThread*, std::function<int()>, std::function<int()>))(const std::function<int()>&, const std::function<int()>&)> > (__f=<optimized out>) at /opt/env/include/c++/10.2.0/bits/invoke.h:60
#38 __invoke<std::_Bind<void (net::EventLoopThread::*(net::EventLoopThread*, std::function<int()>, std::function<int()>))(const std::function<int()>&, const std::function<int()>&)> > (__fn=<optimized out>) at /opt/env/include/c++/10.2.0/bits/invoke.h:95
#39 _M_invoke<0> (this=<optimized out>) at /opt/env/include/c++/10.2.0/thread:264
#40 operator() (this=<optimized out>) at /opt/env/include/c++/10.2.0/thread:271
#41 std::thread::_State_impl<std::thread::_Invoker<std::tuple<std::_Bind<void (net::EventLoopThread::*(net::EventLoopThread*, std::function<int ()>, std::function<int ()>))(std::function<int ()> const&, std::function<int ()> const&)> > > >::_M_run() (this=<optimized out>) at /opt/env/include/c++/10.2.0/thread:215
#42 0x00007f172aca2f70 in execute_native_thread_routine () at ../../../../../libstdc++-v3/src/c++11/thread.cc:80
#43 0x00007f172c1c8dc5 in start_thread () from /lib64/libpthread.so.0
#44 0x00007f172a8e61cd in clone () from /lib64/libc.so.6
(gdb)



#0 0x00007f172a8255f7在/lib64/libc.so.6的raise()中
#1 0x00007f172a826e28位于/lib64/libc.so.6的中止()中
#来自/lib64/libc.so.6的消息()中的0x00007f172a865317
#3 0x00007f172a86cfe1在/lib64/libc.so.6的_int_free()中
#在/opt/env/include/c++/10.2.0/ext/atomicity.h:97的allocate(size=168,this_thread=)中4 0x00007f17f172b92b49d
#5在asio/detail/thread\U info\U base分配(大小=168,此线程=)。hpp:73
#6 asio/impl/handler\U alloc\U hook处的asio\U handler\U分配(大小=168)。ipp:34
#7分配(h=…,s=168)
在asio/detail/handler\u alloc\u helpers。hpp:70
#8在asio/detail/handler\u alloc\u helpers处分配(n=1,this=)。hpp:127
#9在asio/detail/reactive\U socket\U send\U op.hpp处分配(处理程序=…):103
#10异步发送(io\U ex=…,处理程序=…,标志=0,
asio/detail/reactive\u socket\u service\u base处的缓冲区=…,impl=…,this=0x7f17080009f8)。hpp:281
#11运算符()(标志=0,缓冲区=。。。,
handler=,this=)
在asio/基本流插座处。hpp:1005
#12启动(令牌=,
asio/async_结果的初始化=)hpp:151
#13异步启动(令牌=…,启动=)
在asio/async_结果中。hpp:364
#14异步写入(
处理程序=,缓冲区=…,此=0x7f1708143480)
在asio/basic\u stream\u插座处。hpp:855
#15 net/asio_通道上的net::AsioChannel::AsyncSendData(this=0x7f1708143460,packet=…)。cpp:326
#net::AsioChannel::SendPacket(this=,packet=,size=)中的16 0x00007f172b92b964
在net/asio_频道。cpp:284
#17 0x00007f172b93cb1e位于/opt/env/include/c++/10.2.0/bits/shared_ptr_base.h:1324的net::AsioServerHandler::SendPacket(this=,pPacket=…,size=)中
#18 0x00007f172b946831位于/opt/env/include/c++/10.2.0/bits/shared_ptr_base.h:1324的运算符()
#19在/opt/env/include/c++/10.2.0/bits/invoke.h:60处调用impl(uuu f=…)
#20在/opt/env/include/c++/10.2.0/bits/invoke.h:110处调用
#21 std::_函数_处理程序::_M_调用(const std::_Any_数据&)(_函子=…)
at/opt/env/include/c++/10.2.0/bits/std_function.h:291
#22 0x00007f172c42ee26位于/opt/env/include/c++/10.2.0/bits/std_function.h:248的std::function::operator()()const(this=)中
#asio_处理程序_调用中的23 0x00007f172b93b0b4(函数=…)
在asio/handler\u invoke\u hook.hpp:85
#24调用(上下文=…,函数=…)
在asio/detail/handler\u invoke\u helpers.hpp:54
#25完成(处理程序=…,函数=…,此=)
在asio/detail/handler_工作。hpp:424
#26 asio::detail::completion_handler::do_complete(无效*,asio::detail::scheduler_operation*,std::error_code const&,unsigned long)(所有者=0x5011470,基=0x7f15c7b2f4a0)在asio/detail/completion_handler处。hpp:73
#27 0x00007f172b932d88完整(字节_传输=0,ec=…,所有者=0x5011470,这=0x7f15c7b2f4a0)
在asio/detail/scheduler_操作中。hpp:39
#28 asio::详细信息::调度程序::是否运行一个(此=this@entry=0x5011470,锁=…,此线程=…,ec=…)
位于asio/detail/impl/scheduler.ipp:480
#asio::detail::scheduler::run中的29 0x00007f172b934336(this=0x5011470,ec=…)
在asio/detail/impl/scheduler.ipp:203
---键入以继续,或键入q以退出---
#30 0x00007f172b9394c6在asio/impl/io_上下文中运行(此=0x50113c8)。ipp:62
#31 net::AsioEventLoop::在net/asio_事件_循环处运行(this=0x50113c0)。cpp:30
#net::EventLoopThread::Run(std::function const&,std::function const&)中的32 0x00007f172b942ae1(this=0x4fe9ec0,pre=…,post=…)
at/opt/env/include/c++/10.2.0/bits/unique_ptr.h:421
#33 0x00007f172b942e6f在/opt/env/include/c++/10.2.0/bits/invoke.h处的调用impl(\uuuuu t=,\uuuu f=)中:89
#34 u u调用(
__fn=)at/opt/env/include/c++/10.2.0/bits/invoke.h:95
#35在/opt/env/include/c++/10.2.0/functional:416处调用(uuu args=,this=)
#36 operator()(this=)位于/opt/env/include/c++/10.2.0/functional:499
#37在/opt/env/include/c++/10.2.0/bits/invoke.h:60处调用impl(uuu f=)
#38在/opt/env/include/c++/10.2.0/bits/invoke.h:95处调用(uuufn=)
#39_M_invoke(this=)at/opt/env/include/c++/10.2.0/thread:264
#40 operator()(this=)位于/opt/env/include/c++/10.2.0/thread:271
#41 std::thread::_State\u impl::_M_run()(this=)at/opt/env/include/c++/10.2.0/thread:215
#42 0x00007f172aca2f70在../../../../../../../../libstdc++-v3/src/c++11/thread.cc:80处执行本机线程例程()
#43 0x00007f172c1c8dc5位于/lib64/libpthread.so.0的start_线程()中
#来自/lib64/libc.so.6的克隆()中的44 0x00007f172a8e61cd
(gdb)
为什么响应会导致崩溃

  • 不使用Boost
  • 在C++17模式下用g++10.2.0编译
  • 运行GCC10的libstdc++以获得C++17功能支持
  • 在Red Hat Enterprise Linux服务器7.2版(Maipo)上运行
  • 使用
    -O2编译的所有内容

没有asio的专门知识,但分配函数中的崩溃通常意味着内存错误:缓冲区