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

后端 未结 2 904
轮回少年
轮回少年 2020-12-02 01:36

I am writing a simple tcp client to send and receive single lines of text. The asynchronous operations are handled by std::future in order to faciliate blocking queries with

2条回答
  •  [愿得一人]
    2020-12-02 01:48

    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.

提交回复
热议问题