From 48e2faa22e6c8b7f2eca965dd87f7bca760b3b1f Mon Sep 17 00:00:00 2001 From: noosc <534815812@qq.com> Date: Fri, 24 May 2019 16:24:41 +0800 Subject: [PATCH] fix bad_weak_ptr pass weak_ptr of connection to http2_handler, avoid calling `do_write` after connection is destroyed. --- src/asio_server_connection.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/asio_server_connection.h b/src/asio_server_connection.h index daf9a664..f5102c87 100644 --- a/src/asio_server_connection.h +++ b/src/asio_server_connection.h @@ -87,10 +87,15 @@ public: /// Start the first asynchronous operation for the connection. void start() { boost::system::error_code ec; - + std::weak_ptr weak_self = this->shared_from_this(); + handler_ = std::make_shared( GET_IO_SERVICE(socket_), socket_.lowest_layer().remote_endpoint(ec), - [this]() { do_write(); }, mux_); + [weak_self]() { + auto shared_connection = weak_self.lock(); + if (!shared_connection) { return; } + shared_connection->do_write(); + }, mux_); if (handler_->start() != 0) { stop(); return;