[英]Boost::Asio - async_wait in async_accept handler crashes application
I am currently trying to create a server application using Boost::Asio that does two simple things: 我目前正在尝试使用Boost :: Asio创建一个服务器应用程序,该应用程序可以完成两个简单的操作:
boost::asio::deadline_timer
which repeats itself boost::asio::deadline_timer
重复一次 The following code shows my current attempt: 以下代码显示了我当前的尝试:
#define BOOST_ASIO_ENABLE_HANDLER_TRACKING
#include <WinSock2.h>
#include <Mswsock.h>
#include <boost/asio/io_service.hpp>
#include <boost/asio/ip/tcp.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::asio;
using namespace boost::asio::ip;
void timerHandler(const boost::system::error_code& errorCode, deadline_timer* timer) {
timer->expires_at(timer->expires_at() + boost::posix_time::seconds(1));
timer->async_wait(boost::bind(timerHandler, _1, timer));
}
void acceptHandler(const boost::system::error_code &errorCode, io_service *ioService) {
deadline_timer timer(*ioService, boost::posix_time::seconds(1));
timer.async_wait(boost::bind(timerHandler, _1, &timer));
}
int main(int argc, char** argv) {
io_service ioService;
tcp::socket socket(ioService);
tcp::acceptor acceptor{ ioService, tcp::endpoint{ tcp::v4(), 12345 } };
acceptor.listen();
acceptor.async_accept(socket, boost::bind(acceptHandler, _1, &ioService));
ioService.run();
return EXIT_SUCCESS;
}
Problem : 问题 :
The timer somehow does not work as expected in the acceptHandler
. 计时器以某种方式无法在
acceptHandler
中正常工作。 Somehow it gets cancelled twice, triggers an error on top of that and eventually crashes the entire application. 不知何故,它被取消了两次,并触发了一个错误,最终使整个应用程序崩溃。
Handler Tracking Output : 处理程序跟踪输出 :
@asio|1460922050.075890|0*1|socket@000000000015FAD0.async_accept
@asio|1460922051.153952|>1|ec=system:0
@asio|1460922051.153952|1*2|deadline_timer@000000000015F608.async_wait
@asio|1460922051.153952|1|deadline_timer@000000000015F608.cancel
@asio|1460922051.153952|<1|
@asio|1460922051.153952|>2|ec=system:995
@asio|1460922051.153952|2|deadline_timer@000000000015F608.cancel
Questions : 问题 :
acceptHandler
to cancel the deadline_timer in line 4 of the Handler Tracking output? acceptHandler
取消Handler Tracking输出的第4行中的acceptHandler
? What casues the error 995 in line 6 of the Handler Tracking output? 是什么引起处理程序跟踪输出第6行中的错误995? Error message is: The I/O operation has been aborted because of either a thread exit or an application request
错误消息是: 由于线程退出或应用程序请求,I / O操作已中止
What causes the timerHandler to cancel the deadline_timer in line 7 of the Handler Tracking output? 是什么导致timerHandler取消Handler Tracking输出的第7行中的duration_timer?
timer
is allocated on the stack in the acceptHandler
and is therefore not valid by the time timerHandler
is called. timer
是在acceptHandler
的堆栈上分配的,因此在timerHandler
无效。 You need to allocate the timer dynamically. 您需要动态分配计时器。
Also, you should check for error codes in both handlers. 另外,您应该在两个处理程序中检查错误代码。 This is especially important when you want to end the program and
cancel
the timer. 当您要结束程序并
cancel
计时器时,这尤其重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.