fix memory leak in circular shared ptrs

This commit is contained in:
Daniel Larimer 2015-03-30 18:34:04 -04:00
parent e506e4f4be
commit bcd642e31f
2 changed files with 9 additions and 9 deletions

View file

@ -10,7 +10,7 @@ namespace fc { namespace rpc {
class websocket_api_connection : public api_connection class websocket_api_connection : public api_connection
{ {
public: public:
websocket_api_connection( fc::http::websocket_connection_ptr c ) websocket_api_connection( fc::http::websocket_connection& c )
:_connection(c) :_connection(c)
{ {
_rpc_state.add_method( "call", [this]( const variants& args ) -> variant { _rpc_state.add_method( "call", [this]( const variants& args ) -> variant {
@ -19,7 +19,7 @@ namespace fc { namespace rpc {
args[1].as_string(), args[1].as_string(),
args[2].get_array() ); 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, virtual variant send_call( api_id_type api_id,
@ -27,7 +27,7 @@ namespace fc { namespace rpc {
const variants& args = variants() ) override const variants& args = variants() ) override
{ {
auto request = _rpc_state.start_remote_call( "call", {api_id, method_name, args} ); 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 ); 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 ); auto result = _rpc_state.local_call( call.method, call.params );
if( call.id ) 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 ) catch ( const fc::exception& e )
{ {
if( call.id ) 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 ); _rpc_state.handle_reply( reply );
} }
} }
fc::http::websocket_connection_ptr _connection; fc::http::websocket_connection& _connection;
fc::rpc::state _rpc_state; fc::rpc::state _rpc_state;
}; };
} } // namespace fc::rpc } } // namespace fc::rpc

View file

@ -51,7 +51,7 @@ int main( int argc, char** argv )
fc::http::websocket_server server; fc::http::websocket_server server;
server.on_connection([&]( const websocket_connection_ptr& c ){ server.on_connection([&]( const websocket_connection_ptr& c ){
auto wsc = std::make_shared<websocket_api_connection>(c); auto wsc = std::make_shared<websocket_api_connection>(*c);
auto login = std::make_shared<login_api>(); auto login = std::make_shared<login_api>();
login->calc = calc_api; login->calc = calc_api;
wsc->register_api(fc::api<login_api>(login)); wsc->register_api(fc::api<login_api>(login));
@ -66,7 +66,7 @@ int main( int argc, char** argv )
try { try {
fc::http::websocket_client client; fc::http::websocket_client client;
auto con = client.connect( "ws://localhost:8090" ); auto con = client.connect( "ws://localhost:8090" );
auto apic = std::make_shared<websocket_api_connection>(con); auto apic = std::make_shared<websocket_api_connection>(*con);
auto remote_login_api = apic->get_remote_api<login_api>(); auto remote_login_api = apic->get_remote_api<login_api>();
auto remote_calc = remote_login_api->get_calc(); auto remote_calc = remote_login_api->get_calc();
wdump((remote_calc->add( 4, 5 ))); wdump((remote_calc->add( 4, 5 )));