From bcd642e31f5dfcfc9398e478b40c63b91883b4ad Mon Sep 17 00:00:00 2001 From: Daniel Larimer Date: Mon, 30 Mar 2015 18:34:04 -0400 Subject: [PATCH] fix memory leak in circular shared ptrs --- include/fc/rpc/websocket_api.hpp | 14 +++++++------- tests/api.cpp | 4 ++-- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/fc/rpc/websocket_api.hpp b/include/fc/rpc/websocket_api.hpp index aa2f3f6..36bd8e7 100644 --- a/include/fc/rpc/websocket_api.hpp +++ b/include/fc/rpc/websocket_api.hpp @@ -10,7 +10,7 @@ namespace fc { namespace rpc { class websocket_api_connection : public api_connection { public: - websocket_api_connection( fc::http::websocket_connection_ptr c ) + websocket_api_connection( fc::http::websocket_connection& c ) :_connection(c) { _rpc_state.add_method( "call", [this]( const variants& args ) -> variant { @@ -19,7 +19,7 @@ namespace fc { namespace rpc { args[1].as_string(), args[2].get_array() ); }); - _connection->on_message_handler( [&]( const std::string& msg ){ on_message(msg); } ); + _connection.on_message_handler( [&]( const std::string& msg ){ on_message(msg); } ); } virtual variant send_call( api_id_type api_id, @@ -27,7 +27,7 @@ namespace fc { namespace rpc { const variants& args = variants() ) override { auto request = _rpc_state.start_remote_call( "call", {api_id, method_name, args} ); - _connection->send_message( fc::json::to_string(request) ); + _connection.send_message( fc::json::to_string(request) ); return _rpc_state.wait_for_response( *request.id ); } @@ -43,14 +43,14 @@ namespace fc { namespace rpc { auto result = _rpc_state.local_call( call.method, call.params ); if( call.id ) { - _connection->send_message( fc::json::to_string( response( *call.id, result ) ) ); + _connection.send_message( fc::json::to_string( response( *call.id, result ) ) ); } } catch ( const fc::exception& e ) { if( call.id ) { - _connection->send_message( fc::json::to_string( response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) ) ); + _connection.send_message( fc::json::to_string( response( *call.id, error_object{ 1, e.to_detail_string(), fc::variant(e)} ) ) ); } } } @@ -60,8 +60,8 @@ namespace fc { namespace rpc { _rpc_state.handle_reply( reply ); } } - fc::http::websocket_connection_ptr _connection; - fc::rpc::state _rpc_state; + fc::http::websocket_connection& _connection; + fc::rpc::state _rpc_state; }; } } // namespace fc::rpc diff --git a/tests/api.cpp b/tests/api.cpp index 440479c..ec7a2ef 100644 --- a/tests/api.cpp +++ b/tests/api.cpp @@ -51,7 +51,7 @@ int main( int argc, char** argv ) fc::http::websocket_server server; server.on_connection([&]( const websocket_connection_ptr& c ){ - auto wsc = std::make_shared(c); + auto wsc = std::make_shared(*c); auto login = std::make_shared(); login->calc = calc_api; wsc->register_api(fc::api(login)); @@ -66,7 +66,7 @@ int main( int argc, char** argv ) try { fc::http::websocket_client client; auto con = client.connect( "ws://localhost:8090" ); - auto apic = std::make_shared(con); + auto apic = std::make_shared(*con); auto remote_login_api = apic->get_remote_api(); auto remote_calc = remote_login_api->get_calc(); wdump((remote_calc->add( 4, 5 )));