访问冲突发生在第275行的basic_stream_socket.hpp中.原因似乎是对象(boost :: asio :: stream_socket_service)没有初始化(this指针的值是0xfeeefeee),但我不明白为什么事实并非如此.
程序输出:
Start server
Server::startAccept()
Server::handleAccept()
Connection accepted
Connection::startRead()
Server::startAccept()
Connection::handleRead()
READ ERROR: The I/O operation has been aborted because either a thread exited or an application request
Connection::startRead()
代码
#include "@R_989_3013@.h"#include "db.h"class Connection : public boost::enable_shared_from_this<Connection>{public: typedef boost::shared_ptr<Connection> Pointer; static Pointer create(boost::asio::io_service& ioService) { return Pointer(new Connection(ioService)); } ip::tcp::socket& getSocket() { return socket; } voID startRead() { std::cout << "Connection::startRead()" << std::endl; socket.async_read_some(boost::asio::buffer(readBuffer),boost::bind(&Connection::handleRead,this,_1,_2)); }private: Connection(asio::io_service& ioService) : socket(ioService) { } voID handleWrite(const boost::system::error_code&,size_t) { } voID handleRead(const boost::system::error_code&error,size_t len) { std::cout << "Connection::handleRead()" << std::endl; if(error) { std::cout << "READ ERROR: "; std::cout << boost::system::system_error(error).what(); std::cout << std::endl; } else { std::cout << "read: "; std::cout.write(readBuffer.data(),len); std::cout << std::endl; } startRead(); } boost::array<char,256> readBuffer; ip::tcp::socket socket;};class Server{public: Server(asio::io_service& ioService) :acceptor(ioService,ip::tcp::endpoint(ip::tcp::v4(),getPort())) { startAccept(); }private: voID startAccept() { std::cout << "RServer::startAccept()" << std::endl; Connection::Pointer newConn = Connection::create(acceptor.io_service()); acceptor.async_accept(newConn->getSocket(),boost::bind(&Server::handleAccept,newConn,asio::placeholders::error)); } voID handleAccept(Connection::Pointer newConn,const boost::system::error_code& error) { std::cout << "Server::handleAccept()" << std::endl; if(error) { std::cout << "CONNECTION ERROR: "; std::cout << boost::system::system_error(error).what(); std::cout << std::endl; } else { std::cout << "Connection accepted" << std::endl; startAccept(); newConn->startRead(); } } ip::tcp::acceptor acceptor;};int main(){ std::cout << "Start server" << std::endl; asio::io_service ioService; RemoteadminServer server(ioService); boost::system::error_code error; ioService.run(error);}解决方法 您应该更改此代码段:
voID startRead() { std::cout << "Connection::startRead()" << std::endl; socket.async_read_some(boost::asio::buffer(readBuffer),_2)); }
至:
voID startRead(){ std::cout << "Connection::startRead()" << std::endl; socket.async_read_some(boost::asio::buffer(readBuffer),this->shared_from_this(),_2));}
请注意,我将共享指针传递给bind.这将保持Connection实例,直到调用处理程序.否则,在Server :: startAccept中使用计数变为零,并删除该对象.然后,当调用处理程序时,内存无效,您会遇到可怕的“未定义行为”.
总结以上是内存溢出为你收集整理的c – boost :: asio,异步读错误全部内容,希望文章能够帮你解决c – boost :: asio,异步读错误所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)