From 741398826280fe8ea212a0001f9b3d655fff6de9 Mon Sep 17 00:00:00 2001 From: Mika Laitio Date: Fri, 18 Mar 2011 00:59:19 +0200 Subject: [PATCH] small cleanups for error handling and memory leaks --- src/plpbus/BusClient.cc | 43 +++++++++++++++++++++++++++---------- src/plpbus/BusClient.hh | 3 ++- src/plpbus/OrbServerImpl.cc | 7 +++--- src_test/test_client.cc | 4 ++++ 4 files changed, 41 insertions(+), 16 deletions(-) diff --git a/src/plpbus/BusClient.cc b/src/plpbus/BusClient.cc index 004326b..75583fe 100644 --- a/src/plpbus/BusClient.cc +++ b/src/plpbus/BusClient.cc @@ -5,7 +5,9 @@ * Author: lamikr */ +#include #include +#include #include "BusClient.hh" #include "ClientServerCommon.hh" @@ -47,11 +49,12 @@ class OrbClientImpl : public virtual POA_plpbus_orb::OrbClient { } virtual void receive_response_dataitem_sequence(const ::plpbus_orb::DataItemSequence& msg_rsp_param) { - BusMessageInternal *bus_msg; + const BusMessageInternal *bus_msg; if (listener != NULL) { bus_msg = new BusMessageInternal(msg_rsp_param); listener->response_received(bus_msg); + delete(bus_msg); } } }; @@ -65,6 +68,9 @@ BusClient::BusClient() BusClient::~BusClient() { + if (_poa != NULL) { + //_poa->destroy(true, true); + } if (_orb != NULL) { _orb->destroy(); _orb = NULL; @@ -76,12 +82,14 @@ int BusClient::init(const char *server_name) { int argc; char **argv; CORBA::Object_var server_obj; + int ret_val; argc = 0; argv = NULL; - log_info("init() started\n");; + ret_val = PLP_ERR; + log_info("CORBA client init() started\n"); _orb = CORBA::ORB_init(argc, argv); - if (_orb != NULL) { + if (CORBA::is_nil(_orb) == false) { log_debug("ORB_init() done, finding server: %s\n", server_name); server_obj = find_server_object_by_name(_orb, CONST_CONTEXT_NAME__PLPBUS, @@ -90,13 +98,19 @@ int BusClient::init(const char *server_name) { CONST_CONTEXT_NAME__PLPBUS); if (CORBA::is_nil(server_obj) == false) { _server = plpbus_orb::OrbServer::_narrow(server_obj); - log_info("Server object found: %s", server_name); + if (CORBA::is_nil(_server) == false) { + log_info("CORBA service found: %s", server_name); + ret_val = PLP_OK; + } } else { - log_error("Could not find server object: %s\n", server_name); + log_error("Could not find CORBA service: %s\n", server_name); } } - return 0; + else { + log_error("Could not init CORBA client\n"); + } + return ret_val; } int BusClient::add_client_listener(IClientListener *listener_param) { @@ -123,8 +137,11 @@ int BusClient::send_message_and_request_response(const char *msg_req_param) { int BusClient::send_message_and_wait_response(const char *msg_req_param, char **msg_rsp_param) { CORBA::Long ret_val; + char *rsp; - *msg_rsp_param = _server->send_message_and_wait_response(msg_req_param, ret_val); + rsp = _server->send_message_and_wait_response(msg_req_param, ret_val); + *msg_rsp_param = strdup(rsp); + CORBA::string_free(rsp); return ret_val; } @@ -185,13 +202,14 @@ PortableServer::POA_var BusClient::create_poa(CORBA::ORB_var orb) { return ret_val; } -CosNaming::NamingContext_var BusClient::get_name_service_context(CORBA::ORB_var orb_param) { +CosNaming::NamingContext_var BusClient::get_name_service_context(CORBA::ORB_var orb_param, + const char *nameservice_name_param) { CORBA::Object_var ns_obj; CosNaming::NamingContext_var ret_val; ret_val = NULL; try { - ns_obj = orb_param->resolve_initial_references(CONST_NAME_SERVICE_NAME); + ns_obj = orb_param->resolve_initial_references(nameservice_name_param); if (CORBA::is_nil(ns_obj) == false) { // narrow the object reference ret_val = CosNaming::NamingContext::_narrow(ns_obj); @@ -222,9 +240,12 @@ CORBA::Object_var BusClient::find_server_object_by_name(CORBA::ORB_var orb_param CORBA::Object_var ret_val; CosNaming::NamingContext_var ns_root_context; CosNaming::Name service_data; + char *n; - ret_val = CORBA::Object::_nil(); - ns_root_context = get_name_service_context(orb_param); + ret_val = CORBA::Object::_nil(); + n = strdup(CONST_NAME_SERVICE_NAME); + ns_root_context = get_name_service_context(orb_param, n); + free(n); if (CORBA::is_nil(ns_root_context) == false) { // define search criteria service_data.length(2); diff --git a/src/plpbus/BusClient.hh b/src/plpbus/BusClient.hh index 9b32bf8..32ab7ad 100644 --- a/src/plpbus/BusClient.hh +++ b/src/plpbus/BusClient.hh @@ -11,6 +11,7 @@ #include #include + #include "RequestResponseBus.hh" #include "IClientListener.hh" #include "BusMessage.hh" @@ -34,7 +35,7 @@ namespace plpbus { plpbus_orb::OrbClient_var _client; PortableServer::POA_var create_poa(CORBA::ORB_var orb); - CosNaming::NamingContext_var get_name_service_context(CORBA::ORB_var orb_param); + CosNaming::NamingContext_var get_name_service_context(CORBA::ORB_var orb_param, const char *nameservice_name); /** * Finds server object by it's name by using following keywords: * name[0].id = (const char *)CONST_LEAF_CONTEXT_NAME__PLPBUS; diff --git a/src/plpbus/OrbServerImpl.cc b/src/plpbus/OrbServerImpl.cc index 6e09748..9a48aba 100644 --- a/src/plpbus/OrbServerImpl.cc +++ b/src/plpbus/OrbServerImpl.cc @@ -64,13 +64,14 @@ char *OrbServerImpl::send_message_and_wait_response(const char* msg_req_param, : if (_listener != NULL) { log_debug("send_message_and_wait_response(): %s, server_callback != NULL\n", msg_req_param); _listener->request_received(msg_req_param, &msg_rsp); + //ret_val = CORBA::string_dup(msg_rsp); + ret_val = CORBA::string_dup(msg_rsp); } else { log_error("send_message_and_wait_response() error, server callback == NULL\n"); - msg_rsp = strdup(msg_req_param); + ret_val = CORBA::string_dup(msg_req_param); err_flg = -1; } - ret_val = CORBA::string_dup(msg_rsp); return ret_val; } @@ -288,9 +289,7 @@ CosNaming::NamingContext_var OrbServerImpl::get_service_naming_context(const cha } } else { - log_debug("get_service_naming_context() 8\n"); ret_val = CosNaming::NamingContext::_narrow(service_obj); - log_debug("get_service_naming_context() 9\n"); if (CORBA::is_nil(ret_val) ) { log_error("Failed to get existing context from name service for %s, narrowing failed.\n", service_name_param); } diff --git a/src_test/test_client.cc b/src_test/test_client.cc index 64d06bc..26f50c8 100644 --- a/src_test/test_client.cc +++ b/src_test/test_client.cc @@ -4,6 +4,7 @@ * Created on: Jun 9, 2010 * Author: lamikr */ +#include #include #include "plpbus/BusServer.hh" @@ -78,6 +79,7 @@ int main(int argc, char** argv) rsp = NULL; err_flg = client->send_message_and_wait_response(argv[2], &rsp); log_debug("rsp: %s\n", rsp); + free(rsp); err_flg = client->send_message_and_request_response(argv[2]); if (err_flg == 0) { log_debug("request message send successfully\n"); @@ -93,6 +95,8 @@ int main(int argc, char** argv) else { log_error("client failed to init\n"); } + delete(client); + delete(lstnr); } else { log_info("usage: \n"); -- 2.41.1