0
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?!

Abanoub
  • 3,623
  • 16
  • 66
  • 104

2 Answers2

2

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.

Community
  • 1
  • 1
Simon Richter
  • 28,572
  • 1
  • 42
  • 64
  • yeah your right idk how i froget that, but still when i made the new vector before the method it's break Error `vector iterator not dereferencabel` – Abanoub May 23 '11 at 12:11
  • Yes, that is where the other answer comes in -- `begin()` returns an iterator to the first element, or to one-past-the-end if there are no elements. Since you have not inserted an element yet, the second case applies, and the debug code finds that you are trying to dereference a singular iterator. – Simon Richter May 23 '11 at 12:22
  • so ummm how can i fix this issue? – Abanoub May 23 '11 at 12:28
1

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(), 
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • sorry i didnt get what you are trying to tell me! – Abanoub May 23 '11 at 12:33
  • @MixedCoder - That the access violation happens when trying to deference the pointer. The value 0xcccccccc is used by the debugger to mark uninitalized memory. The value 0xccccccd0 is the result of accessing something a few bytes offset from that pointer, like in `front()->socket()`. The real problem is likely that `m_connections` is totally empty. – Bo Persson May 23 '11 at 12:37
  • @MixedCoder - Somehow you will have to create `Connection`s and put them in the vector. Perhaps that is in the code you haven't shown? Otherwise you will have to add it. – Bo Persson May 23 '11 at 13:58