Vector: Unhandled exception at 0x00066314 in AccountServer.exe: 0xC0000005: Access violation reading location 0xccccccd0
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));
}
it builds the project with no errors but when am trying to run the program it's break and gives me this error
Unhandled exception at 0x00066314 in AccountServer.exe: 0xC0000005: Access violation reading location 0xccccccd0.
what's wrong here?!
Assuming Visual C++ here, I think this question may be related; you are trying to dereference an uninitialized pointer on the stack.
Specifically, you are invoking start_accept() before you initialize the pointer to the vector; apparently, your Server object lives on the stack, and the first field in the vector structure to be accessed lives at offset 4.
This line
m_connections = new std::vector<Connection*>();
create a vector of pointers. When are the pointers initalized?
Here they are assumed to point to someting with a socket()
acceptor_.async_accept(m_connections->front()->socket(),
精彩评论