From d0c457008b8ca4d188f5c66b62e6265f643ec9ae Mon Sep 17 00:00:00 2001 From: Mu-Woong Lee Date: Mon, 7 Aug 2017 11:34:09 +0900 Subject: [PATCH] Fix memory leaks: ServiceRunner::~ServiceRunner deletes its service instance Change-Id: Icff41cd89140d58fd60ad6ea92fb939dcc2bf047 Signed-off-by: Mu-Woong Lee --- src/server/ServiceRunner.cpp | 49 ++++++++++++++++++++++---------------------- src/server/ServiceRunner.h | 4 ++-- 2 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/server/ServiceRunner.cpp b/src/server/ServiceRunner.cpp index eb0b058..1bcddb6 100644 --- a/src/server/ServiceRunner.cpp +++ b/src/server/ServiceRunner.cpp @@ -25,8 +25,8 @@ using namespace ctx; ServiceRunner::ServiceRunner(GDBusConnection* conn, IService* service) : - serviceInstance(service), - started(false), + __serviceInstance(service), + __started(false), __uid(0), __connection(conn), __objPath(CTX_DBUS_PATH "/"), @@ -34,12 +34,13 @@ ServiceRunner::ServiceRunner(GDBusConnection* conn, IService* service) : __nodeInfo(NULL), __registrationId(0) { - __objPath += serviceInstance->getServiceName(); - __interface += serviceInstance->getServiceName(); + __objPath += __serviceInstance->getServiceName(); + __interface += __serviceInstance->getServiceName(); } ServiceRunner::~ServiceRunner() { + delete __serviceInstance; } GMainContext* ServiceRunner::getMainContext() @@ -54,14 +55,14 @@ GDBusConnection* ServiceRunner::getConnection() IService* ServiceRunner::getService() { - return serviceInstance; + return __serviceInstance; } bool ServiceRunner::start(uid_t uid) { - IF_FAIL_RETURN(!started, true); + IF_FAIL_RETURN(!__started, true); - _I(CYAN("Starting '%s'"), serviceInstance->getServiceName()); + _I(CYAN("Starting '%s'"), __serviceInstance->getServiceName()); if (!__init(uid)) { _E("Starting failed"); @@ -69,17 +70,17 @@ bool ServiceRunner::start(uid_t uid) return false; } - started = true; + __started = true; __uid = uid; return true; } void ServiceRunner::stop() { - IF_FAIL_VOID(started); - started = false; + IF_FAIL_VOID(__started); + __started = false; - _I(PURPLE("Stopping '%s'"), serviceInstance->getServiceName()); + _I(PURPLE("Stopping '%s'"), __serviceInstance->getServiceName()); __release(__uid); } @@ -103,7 +104,7 @@ bool ServiceRunner::__init(uid_t uid) vtable.set_property = NULL; std::string introspection("" + serviceInstance->getMethodSpecs() + ""; + introspection += __interface + "'>" + __serviceInstance->getMethodSpecs() + ""; __nodeInfo = g_dbus_node_info_new_for_xml(introspection.c_str(), NULL); IF_FAIL_RETURN_TAG(__nodeInfo, false, _E, "NodeInfo creation failed"); @@ -161,9 +162,9 @@ ServiceClient* ServiceRunner::__getClient(const std::string& busName) ServiceClient* client = __createClient(busName); IF_FAIL_RETURN(client, NULL); - IMethodCallHandler* callHandler = serviceInstance->createMethodCallHandler(client); + IMethodCallHandler* callHandler = __serviceInstance->createMethodCallHandler(client); if (!callHandler) { - _E("%s's method call handler cannot be NULL.", serviceInstance->getServiceName()); + _E("%s's method call handler cannot be NULL.", __serviceInstance->getServiceName()); delete client; return NULL; } @@ -186,7 +187,7 @@ void ServiceRunner::__onNameOwnerChanged(GDBusConnection* conn, const gchar* sen void ServiceRunner::__removeClient(const std::string& busName) { - _I("'%s' lost '%s'", serviceInstance->getServiceName(), busName.c_str()); + _I("'%s' lost '%s'", __serviceInstance->getServiceName(), busName.c_str()); auto iter = __clients.find(busName); IF_FAIL_VOID(iter != __clients.end()); @@ -219,24 +220,24 @@ SystemServiceRunner::SystemServiceRunner(GDBusConnection* conn, IService* servic void SystemServiceRunner::notifyUserNew(uid_t uid) { - if (started) - static_cast(serviceInstance)->onUserActivated(uid); + if (__started) + static_cast(__serviceInstance)->onUserActivated(uid); } void SystemServiceRunner::notifyUserRemoved(uid_t uid) { - if (started) - static_cast(serviceInstance)->onUserDeactivated(uid); + if (__started) + static_cast(__serviceInstance)->onUserDeactivated(uid); } bool SystemServiceRunner::__prepare(uid_t uid) { - return static_cast(serviceInstance)->prepare(); + return static_cast(__serviceInstance)->prepare(); } void SystemServiceRunner::__cleanup(uid_t uid) { - static_cast(serviceInstance)->cleanup(); + static_cast(__serviceInstance)->cleanup(); } ServiceClient* SystemServiceRunner::__createClient(const std::string& busName) @@ -266,12 +267,12 @@ void UserServiceRunner::notifyUserRemoved(uid_t uid) bool UserServiceRunner::__prepare(uid_t uid) { - return static_cast(serviceInstance)->prepare(uid); + return static_cast(__serviceInstance)->prepare(uid); } void UserServiceRunner::__cleanup(uid_t uid) { - static_cast(serviceInstance)->cleanup(uid); + static_cast(__serviceInstance)->cleanup(uid); } ServiceClient* UserServiceRunner::__createClient(const std::string& busName) @@ -284,7 +285,7 @@ ServiceClient* UserServiceRunner::__createClient(const std::string& busName) } if (!util::is_normal_user(client->getUid())) { - _W("%s does not support container users.", serviceInstance->getServiceName()); + _W("%s does not support container users.", __serviceInstance->getServiceName()); delete client; return NULL; } diff --git a/src/server/ServiceRunner.h b/src/server/ServiceRunner.h index 0e7cf3d..d291f59 100644 --- a/src/server/ServiceRunner.h +++ b/src/server/ServiceRunner.h @@ -46,8 +46,8 @@ namespace ctx { protected: ServiceRunner(GDBusConnection* conn, IService* service); - IService* serviceInstance; - bool started; + IService* __serviceInstance; + bool __started; private: static void __onMethodCalled(GDBusConnection* conn, const gchar* sender, -- 2.7.4