From 4309749dd98937b8a7be5dc0bfe679ba201c5512 Mon Sep 17 00:00:00 2001 From: Peter Thorson Date: Sun, 19 Oct 2014 08:56:07 -0400 Subject: [PATCH] switch to alternate method of clearing function objects may help with null_ptr related issues when using boost with c++11 compilers --- websocketpp/common/functional.hpp | 12 ++++++++++-- websocketpp/impl/endpoint_impl.hpp | 4 ++-- websocketpp/transport/asio/connection.hpp | 14 +++++++------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/websocketpp/common/functional.hpp b/websocketpp/common/functional.hpp index 670e211ef4..2855d3fc18 100644 --- a/websocketpp/common/functional.hpp +++ b/websocketpp/common/functional.hpp @@ -49,7 +49,11 @@ namespace lib { using std::bind; using std::ref; namespace placeholders = std::placeholders; - #define _WEBSOCKETPP_NULL_FUNCTION_ nullptr + + template + void clear_function(T & x) { + x = nullptr; + } #else using boost::function; using boost::bind; @@ -59,7 +63,11 @@ namespace lib { using ::_1; using ::_2; } - #define _WEBSOCKETPP_NULL_FUNCTION_ 0 + + template + void clear_function(T & x) { + x.clear(); + } #endif } // namespace lib diff --git a/websocketpp/impl/endpoint_impl.hpp b/websocketpp/impl/endpoint_impl.hpp index 072e12c6d9..2c53914b91 100644 --- a/websocketpp/impl/endpoint_impl.hpp +++ b/websocketpp/impl/endpoint_impl.hpp @@ -42,8 +42,8 @@ endpoint::create_connection() { //scoped_lock_type guard(m_mutex); // Create a connection on the heap and manage it using a shared pointer - connection_ptr con = lib::make_shared(m_is_server,m_user_agent,m_alog, - m_elog, m_rng); + connection_ptr con = lib::make_shared(m_is_server, + m_user_agent, m_alog, m_elog, m_rng); connection_weak_ptr w(con); diff --git a/websocketpp/transport/asio/connection.hpp b/websocketpp/transport/asio/connection.hpp index e2a7acfb8d..7969226c3e 100644 --- a/websocketpp/transport/asio/connection.hpp +++ b/websocketpp/transport/asio/connection.hpp @@ -440,8 +440,8 @@ protected: if (ec) { // reset the handlers to break the circular reference: // this->handler->this - m_async_read_handler = _WEBSOCKETPP_NULL_FUNCTION_; - m_async_write_handler = _WEBSOCKETPP_NULL_FUNCTION_; + lib::clear_function(m_async_read_handler); + lib::clear_function(m_async_write_handler); } return ec; @@ -994,12 +994,12 @@ protected: // Reset cached handlers now that we won't be reading or writing anymore // These cached handlers store shared pointers to this connection and // will leak the connection if not destroyed. - m_async_read_handler = _WEBSOCKETPP_NULL_FUNCTION_; - m_async_write_handler = _WEBSOCKETPP_NULL_FUNCTION_; - m_init_handler = _WEBSOCKETPP_NULL_FUNCTION_; + lib::clear_function(m_async_read_handler); + lib::clear_function(m_async_write_handler); + lib::clear_function(m_init_handler); - m_read_handler = _WEBSOCKETPP_NULL_FUNCTION_; - m_write_handler = _WEBSOCKETPP_NULL_FUNCTION_; + lib::clear_function(m_read_handler); + lib::clear_function(m_write_handler); timer_ptr shutdown_timer; shutdown_timer = set_timer(