From c474692329927f7a089851d12ca7fe4abd5c24cb Mon Sep 17 00:00:00 2001 From: Sergey Semushin Date: Thu, 4 Jul 2019 23:28:43 +0300 Subject: [PATCH] Fix lifetime of 'frame' buffer in tcp server/client. --- SourceX/dvlnet/tcp_client.cpp | 10 ++++++---- SourceX/dvlnet/tcp_server.cpp | 12 +++++++----- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/SourceX/dvlnet/tcp_client.cpp b/SourceX/dvlnet/tcp_client.cpp index 51e2e4ab0..16a718571 100644 --- a/SourceX/dvlnet/tcp_client.cpp +++ b/SourceX/dvlnet/tcp_client.cpp @@ -97,10 +97,12 @@ void tcp_client::handle_send(const asio::error_code& error, size_t bytes_sent) void tcp_client::send(packet& pkt) { - auto frame = frame_queue::make_frame(pkt.data()); - asio::async_write(sock, asio::buffer(frame), - std::bind(&tcp_client::handle_send, this, - std::placeholders::_1, std::placeholders::_2)); + auto frame = std::make_shared(frame_queue::make_frame(pkt.data())); + auto buf = asio::buffer(*frame); + asio::async_write(sock, buf, [this, frame = std::move(frame)] + (const asio::error_code &error, size_t bytes_sent) { + handle_send(error, bytes_sent); + }); } } // namespace net diff --git a/SourceX/dvlnet/tcp_server.cpp b/SourceX/dvlnet/tcp_server.cpp index 2fe1a4f2c..2ff81620d 100644 --- a/SourceX/dvlnet/tcp_server.cpp +++ b/SourceX/dvlnet/tcp_server.cpp @@ -135,11 +135,13 @@ void tcp_server::send_packet(packet& pkt) void tcp_server::start_send(scc con, packet& pkt) { - auto frame = frame_queue::make_frame(pkt.data()); - asio::async_write(con->socket, asio::buffer(frame), - std::bind(&tcp_server::handle_send, this, con, - std::placeholders::_1, std::placeholders::_2)); - + auto frame = std::make_shared(frame_queue::make_frame(pkt.data())); + auto buf = asio::buffer(*frame); + asio::async_write(con->socket, buf, + [this, con, frame = std::move(frame)] + (const asio::error_code &ec, size_t bytes_sent) { + handle_send(con, ec, bytes_sent); + }); } void tcp_server::handle_send(scc con, const asio::error_code& ec,