矢量:AccountServer.exe中0x00066314处未处理的异常:0xC0000005:读取位置0xcccc

Vector: Unhandled exception at 0x00066314 in AccountServer.exe: 0xC0000005: Access violation reading location 0xccccccd0

本文关键字:0xC0000005 异常 读取 位置 0xcccc exe AccountServer 0x00066314 矢量 未处理      更新时间:2023-10-16
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.

这里怎么了?!

假设这里是Visual C++,我认为这个问题可能是相关的;您正试图取消引用堆栈上未初始化的指针。

具体来说,在初始化指向向量的指针之前,您将调用start_accept((;显然,您的Server对象位于堆栈上,而要访问的向量结构中的第一个字段位于偏移量4处。

此行

m_connections = new std::vector<Connection*>(); 

创建一个指针向量。指针何时初始化?

在这里,假设它们指向socket()

acceptor_.async_accept(m_connections->front()->socket(), 
相关文章: