Boost async_accept 会导致“访问冲突” - 错误
Server::Server(boost::asio::io_service& io_service,std::string ip,short port,std::shared_ptr<ConnectionFactory> factory)
: acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(ip.data()), port)){
m_factory = factory;
start_accept();
std::cout<<"Socket accepting connections..."<<std::endl;
}
void Server::start_accept(){
boost::asio::io_service io_service;
std::shared_ptr<Connection> conn = m_factory->create(io_service);
acceptor_.async_accept(conn->socket(),
boost::bind(&Server::handle_accept, this,conn,
boost::asio::placeholders::error));
}
void Server::handle_accept(std::shared_ptr<Connection> conn,const boost::system::error_code& error){
if (!error)
{
std::cout<<"on connected"<<std::endl;
conn->OnConnected();
start_accept();
}
}
当我运行该项目时,出现以下错误:
访问冲突读取位置 0xfeeeff02。
此错误的原因是什么?
Server::Server(boost::asio::io_service& io_service,std::string ip,short port,std::shared_ptr<ConnectionFactory> factory)
: acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::address_v4::from_string(ip.data()), port)){
m_factory = factory;
start_accept();
std::cout<<"Socket accepting connections..."<<std::endl;
}
void Server::start_accept(){
boost::asio::io_service io_service;
std::shared_ptr<Connection> conn = m_factory->create(io_service);
acceptor_.async_accept(conn->socket(),
boost::bind(&Server::handle_accept, this,conn,
boost::asio::placeholders::error));
}
void Server::handle_accept(std::shared_ptr<Connection> conn,const boost::system::error_code& error){
if (!error)
{
std::cout<<"on connected"<<std::endl;
conn->OnConnected();
start_accept();
}
}
When I run the project I get following error:
Access violation reading location 0xfeeeff02.
What is the cause of this error?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 io_service 超出了 start_accept() 的范围,这不好,而且可能不是您想要的。
将此更改
为此
尽管,正如您的评论所建议的,您确实应该发布一个独立的问题示例。上述建议是我最好的猜测。
Your
io_service
is going out of scope instart_accept()
, that is not good and likely not what you intend.change this
to this
Though, as your comments have suggested, you really should post a self-contained example of the problem. The above suggestion is my best guess.