Boost ASIO async_write "Vector iterator is not being played."

I was working on an asynchronous server program and so far I have been able to connect. However, I now get the error "Vector iterator, not undecidable".

I suspect the vector will be destroyed or dereferenced before it sends the packet, resulting in an 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 is called after connection. package.write () returns a vector uint8_t

It would be important if I change

void send(Packet packet)

      

to

void send(Packet& packet)

      

Not in relation to this problem, but in terms of performance.

+2


a source to share


2 answers


I found a solution since the vector will be destroyed. I made a queue that contains the resulting packets and they are processed one by one, now nothing is torn, so the problem is solved.



could change my queue to store the batch class instead of the result, but that's just a detail.

0


a source


It all depends on how your package class is implemented. How is this copied, .... Does the instance of the Packet class have a deep copy, or just a default copy? if it is a default copy and your Packet is not a POD, that might be the reason and you will need to make a deep copy.

In general it is better to pass the class parameter const and so maybe you should try



void send(Packet const& packet);

      

0


a source







All Articles