From 337bf2fb0a86145e8c745d57c2db46cf5904e14b Mon Sep 17 00:00:00 2001 From: Aydan Yumerefendi Date: Mon, 3 Mar 2014 11:08:24 -0500 Subject: [PATCH] Fix memory leak on create If the socket fails to initialize, e.g., by omitting the tis init handler, the connection is leaked, since there is a circular reference between the connection and its async_read/async_write handlers. To fix this, check the error code, and reset the handler is an error has occurred. --- websocketpp/transport/asio/connection.hpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index 71ef330190..dec03b2999 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -443,8 +443,15 @@ protected: m_async_write_handler = lib::bind(&type::handle_async_write, get_shared(), lib::placeholders::_1, lib::placeholders::_2); } - - return socket_con_type::init_asio(io_service, m_strand, m_is_server); + + lib::error_code ec = socket_con_type::init_asio(io_service, m_strand, m_is_server); + if (ec) { + // reset the handlers to break the circular reference: this->handler->this + m_async_read_handler = _WEBSOCKETPP_NULLPTR_TOKEN_; + m_async_write_handler = _WEBSOCKETPP_NULLPTR_TOKEN_; + } + + return ec; } void handle_pre_init(lib::error_code const & ec) {