[英]Boost.Asio UDP async_read_from segmentation fault
I'm building an asynchronous UDP socket and managing it with timer using boost. 我正在构建一个异步UDP套接字,并使用boost使用计时器对其进行管理。 The second time I try to read data from the socket using socket.async_read_from, I'm getting a segmentation fault.
第二次尝试使用socket.async_read_from从套接字读取数据时,出现分段错误。 (Using netbeans and the debugger doesn't seem to do anything...).
(使用netbeans和调试器似乎什么也没做...)。 The first time I read works well.
我第一次阅读效果很好。 Netbeans just throws some assembly code.
Netbeans只是抛出一些汇编代码。 I can't even make a break point work.
我什至无法使断点起作用。 Is there something I'm missing?
有什么我想念的吗? I checked the address of every objects sent to async_read_from and everything seem legit... The first call to readData() works well, so I'm guessing it has something to do with the io_service?
我检查了发送到async_read_from的每个对象的地址,并且一切看起来都是合法的。readData()的第一个调用运行良好,所以我猜测它与io_service有关吗?
bool ServerInstance::openServer()
{
try{
io_service io_service;
this->endpoint_= new ip::udp::endpoint(ip::udp::v4(),nPortNumber_);
this->socket_ = new ip::udp::socket(io_service, *(this->endpoint_));
// this->socket_->non_blocking(false);
this->readData();
}catch(std::exception &e)
{
this->strErrorMsg_ = e.what();
return false;
}
return true;
}
char* readData()
{boost::array<char,80> buf;
boost::system::error_code ec = boost::asio::error::would_block;
this->startTimer();
socket_->async_receive_from(buffer(buf),*(this->endpoint_),
boost::bind(&ServerInstance::handle_read,_1,&ec));
while(ec == boost::asio::error::would_block)
{
socket_->get_io_service().run_one();
}
this->stopTimer();
socket_->get_io_service().reset();
return buf.data();
}
When the socket is created, I thought the io_service
object would be copied but alas no. 创建套接字时,我以为会复制
io_service
对象,但可惜没有。 Since it was declared locally, it is destroyed after the establishConnection()
method completes. 由于它是在本地声明的,因此它会在
establishConnection()
方法完成后销毁。 Declared it as a global pointer and it's working great now. 宣布它为全局指针,并且现在运行良好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.