boost::asio + std::future – Access violation after closing socket

recvmsg is receiving into a buffer (streambuf) that was freed after throwing the exception in TCPClient::sendMessage (line 105, end of scope).

You forgot to cancel the asynchronous operation (async_read_until) started in line 97. Fix it:

else {
    socket->cancel(); // ADDED
    std::cout << "socket points to " << std::addressof(*socket) << std::endl;
    throw std::runtime_error("timeout");
}

Or even, just

    socket.reset(); // ADDED

Same goes for other timeout paths.

Leave a Comment