From 3ca53d006407fd3d85604432d38fa42153b3732c Mon Sep 17 00:00:00 2001 From: Aydan Yumerefendi Date: Mon, 3 Mar 2014 21:51:56 -0500 Subject: [PATCH] Avoid leak when stopping server If accept fails, e.g., because the server is no longer listening the connection object is not terminated and it results in a memory leak: connection -> handler -> connection. --- websocketpp/roles/server_endpoint.hpp | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/websocketpp/roles/server_endpoint.hpp b/websocketpp/roles/server_endpoint.hpp index b5fb0be3d3..9ebb109897 100644 --- a/websocketpp/roles/server_endpoint.hpp +++ b/websocketpp/roles/server_endpoint.hpp @@ -104,7 +104,13 @@ public: endpoint_type::m_elog.write(log::elevel::rerror, "start_accept error: "+ec.message()); } - } + + if (ec) { + // Terminate the connection to prevent memory leaks. + lib::error_code con_ec; + con->terminate(con_ec); + } + } void handle_accept(connection_ptr con, lib::error_code const & ec) { if (ec) {