class Connection
{
public:
explicit Connection(boost::asio::io_service& io_service);
virtual ~Connection();
boost::asio::ip::tcp::socket& socket();
virtual void OnConnected()=0;
void Send(uint8_t* buffer, int length);
bool Receive();
private:
void handler(const boost::system::error_code& error, std::size_t bytes_transferred );
boost::asio::ip::tcp::socket socket_;
};
-----------------------------------------------------------------------------------
Server::Server(boost::asio::io_service& io_service,short port)
: acceptor_(io_service, boost::asio::ip::tcp::endpoint(boost::asio::ip::tcp::v4(), port)){
m_connections = new std::vector<Connection*>();
start_accept();
std::cout<<"Socket accepting connections..."<<std::endl;
}
Server::~Server()
{
m_connections->clear();
delete m_connections;
}
void Server::start_accept(){
/* Connection::pointer new_connection =
Connection::create(acceptor_.io_service());*/
acceptor_.async_accept(m_connections->front()->socket(),
boost::bind(&Server::handle_accept, this, m_connections,
boost::asio::placeholders::error));
}
它构建项目没有错误,但是当我试图运行程序时,它会中断并给我这个错误
Unhandled exception at 0x00066314 in AccountServer.exe: 0xC0000005: Access violation reading location 0xccccccd0.
这里有什么问题?!
答案 0 :(得分:2)
在这里假设Visual C ++,我认为this question可能是相关的;你试图在堆栈上取消引用一个未初始化的指针。
具体来说,在初始化向量指针之前调用start_accept();显然,您的Server对象存在于堆栈中,并且要访问的向量结构中的第一个字段位于偏移量4处。
答案 1 :(得分:1)
这一行
m_connections = new std::vector<Connection*>();
创建一个指针向量。什么时候指针初始化?
这里假设它们指向具有socket()
acceptor_.async_accept(m_connections->front()->socket(),