我有以下代码:
void GnutellaApp::HandleRead (boost::asio::ip::tcp::socket& socket)
{
char buf[128];
try {
boost::asio::async_read( socket,
boost::asio::buffer(buf),
boost::asio::transfer_all(),
boost::bind(&GnutellaApp::HandleRead,
this,
boost::ref(socket)
)
);
}
catch (boost::system::system_error const& e)
{
std::cout << "Warning: " << e.what() << std::endl;
}
}
当调用HandleRead
并执行async_read
时,它会抛出异常。为了异步等待下一个请求,我们需要再次调用async_read
,对吧?
第一次呼叫async_read
成功。
我正在接受例外
我遇到了异常
Gnutella.exe!std::_Ref_count_base::_Incwref() Line 113 C++
Gnutella.exe!std::_Ptr_base<void>::_Resetw<void>(void * _Other_ptr, std::_Ref_count_base * _Other_rep) Line 458 C++
Gnutella.exe!std::_Ptr_base<void>::_Resetw<void>(const std::_Ptr_base<void> & _Other) Line 445 C++
Gnutella.exe!std::weak_ptr<void>::weak_ptr<void><void>(const std::shared_ptr<void> & _Other, void * * __formal) Line 994 C++
Gnutella.exe!boost::asio::detail::win_iocp_socket_service_base::async_receive<boost::asio::mutable_buffers_1,boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >(boost::asio::detail::win_iocp_socket_service_base::base_implementation_type & impl, const boost::asio::mutable_buffers_1 & buffers, int flags, boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > & handler) Line 292 C++
Gnutella.exe!boost::asio::stream_socket_service<boost::asio::ip::tcp>::async_receive<boost::asio::mutable_buffers_1,boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >(boost::asio::detail::win_iocp_socket_service<boost::asio::ip::tcp>::implementation_type & impl, const boost::asio::mutable_buffers_1 & buffers, int flags, boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > && handler) Line 359 C++
Gnutella.exe!boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >::async_read_some<boost::asio::mutable_buffers_1,boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > >(const boost::asio::mutable_buffers_1 & buffers, boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > > && handler) Line 846 C++
Gnutella.exe!boost::asio::detail::read_op<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > >::operator()(const boost::system::error_code & ec, unsigned int bytes_transferred, int start) Line 273 C++
Gnutella.exe!boost::asio::async_read<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> >,boost::asio::mutable_buffers_1,boost::asio::detail::transfer_all_t,boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > >(boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > & s, const boost::asio::mutable_buffers_1 & buffers, boost::asio::detail::transfer_all_t completion_condition, boost::_bi::bind_t<void,boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>,boost::_bi::list2<boost::_bi::value<GnutellaApp *>,boost::reference_wrapper<boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > > > > && handler) Line 535 C++
Gnutella.exe!GnutellaApp::HandleRead(boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > & socket) Line 232 C++
> Gnutella.exe!boost::_mfi::mf1<void,GnutellaApp,boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > &>::operator()(GnutellaApp * p, boost::asio::basic_stream_socket<boost::asio::ip::tcp,boost::asio::stream_socket_service<boost::asio::ip::tcp> > & a1) Line 165 C++
Gnutella.exe!boost::_bi::list2<boost::_bi::value
答案 0 :(得分:3)
看起来好像你持有陈旧的参考资料。
此外,boost::asio::buffer(buf)
是一个明显的攻击,因为buf
是一个堆栈本地,但async_read
是......好 async < /强>
常见的模式是
buf
和socket
类成员(这样他们肯定会比异步请求更长)使buf
和socket
共享(boost::shared_ptr<boost::asio::ip::tcp::socket>
)您可以通过值传递/绑定:
boost::bind(&GnutellaApp::HandleRead,
this,
socket
)
只要绑定函数调用在io服务中保持活动状态,就会保持套接字生效,凭借shared_ptr