首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >向量:AccountServer.exe中0x00066314处的未处理异常: 0xC0000005:访问冲突读取位置0xcccccd0

向量:AccountServer.exe中0x00066314处的未处理异常: 0xC0000005:访问冲突读取位置0xcccccd0
EN

Stack Overflow用户
提问于 2011-05-23 19:56:14
回答 2查看 1.5K关注 0票数 0
代码语言:javascript
复制
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));

}

它没有错误地构建项目,但当我试图运行程序时,它中断了,并给我这个错误

代码语言:javascript
复制
Unhandled exception at 0x00066314 in AccountServer.exe: 0xC0000005: Access violation reading location 0xccccccd0.

这是怎么了?!

EN

回答 2

Stack Overflow用户

发布于 2011-05-23 20:05:31

假设这里是Visual C++,我认为this question可能是相关的;您正在尝试取消对堆栈上未初始化指针的引用。

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

票数 2
EN

Stack Overflow用户

发布于 2011-05-23 20:02:05

这一行

代码语言:javascript
复制
m_connections = new std::vector<Connection*>(); 

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

这里假设它们指向带有socket()的某项内容

代码语言:javascript
复制
acceptor_.async_accept(m_connections->front()->socket(), 
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6096818

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档