tags:

views:

138

answers:

2

Hey,

I've been working on an async boost server program, and so far I've got it to connect. However I'm now getting a "Vector iterator not dereferencable" error.

I suspect the vector gets destroyed or dereferenced before he packet gets sent thus causing the error.

void start()
{
    Packet packet;
    packet.setOpcode(SMSG_PING);
    send(packet);
}

void send(Packet packet)
{
    cout << "DEBUG> Transferring packet with opcode " << packet.GetOpcode() << endl;
    async_write(m_socket, buffer(packet.write()), boost::bind(&Session::writeHandler, shared_from_this(), placeholders::error, placeholders::bytes_transferred));
}

void writeHandler(const boost::system::error_code& errorCode, size_t bytesTransferred)
{
    cout << "DEBUG> Transfered " << bytesTransferred << " bytes to " << m_socket.remote_endpoint().address().to_string() << endl;
}

Start gets called once a connection is made. packet.write() returns a uint8_t vector

Would it matter if I'd change

void send(Packet packet)

to

void send(Packet& packet)

Not in relation to this problem but performance wise.

A: 

All this depends on how your Packet class is implemented. How it is copied, .... Has the copy of Packet class do a deep copy or just a default copy? if it is a default copy and your class Packet is not a POD, this can be the reason, and you will need to do a deep copy.

In general it is better to pass a class parameter by const& so maybe you should try with

void send(Packet const& packet);
Vicente Botet Escriba
A: 

I have found a solution, as the vector would get destroyed I made a queue that contains the resulting packets and they get processed one by one, now nothing gets dereferenced so the problem is solved.

might want to change my queue to hold the packet class instead of the result but that's just a detail.

Xeross