From 93ed93e1131c159a6e04ce9d6cd48eb58b16bfca Mon Sep 17 00:00:00 2001 From: Somin Kim Date: Mon, 16 May 2016 15:51:22 +0900 Subject: [PATCH] Remove memory leak - Release enabled rule instances Change-Id: I0316530fe72395623fd287d6529ef6fb7aa9a01e Signed-off-by: Somin Kim --- src/access_control/PeerCreds.cpp | 1 + src/trigger/ContextMonitor.cpp | 20 ++++++++++---------- src/trigger/ContextMonitor.h | 4 ++-- src/trigger/RuleManager.cpp | 13 +++++++++++++ src/trigger/Trigger.cpp | 2 ++ 5 files changed, 28 insertions(+), 12 deletions(-) diff --git a/src/access_control/PeerCreds.cpp b/src/access_control/PeerCreds.cpp index 4fee21e..35fd152 100644 --- a/src/access_control/PeerCreds.cpp +++ b/src/access_control/PeerCreds.cpp @@ -66,6 +66,7 @@ bool ctx::peer_creds::get(GDBusConnection *connection, const char *uniqueName, c *creds = new(std::nothrow) Credentials(packageId, client, session, user); IF_FAIL_CATCH_TAG(*creds, _E, "Memory allocation failed"); + g_free(app_id); return true; CATCH: diff --git a/src/trigger/ContextMonitor.cpp b/src/trigger/ContextMonitor.cpp index 56f5eed..5dab7f5 100644 --- a/src/trigger/ContextMonitor.cpp +++ b/src/trigger/ContextMonitor.cpp @@ -50,9 +50,9 @@ ContextMonitor::~ContextMonitor() { } -void ContextMonitor::setContextManager(ContextManager* ctx_mgr) +void ContextMonitor::setContextManager(ContextManager* ctxMgr) { - __contextMgr = ctx_mgr; + __contextMgr = ctxMgr; } ContextMonitor* ContextMonitor::getInstance() @@ -190,7 +190,7 @@ bool ContextMonitor::isAllowed(const char *client, const char *subject) int ContextMonitor::__findSub(RequestType type, const char* subject, Json* option) { // @return request id - std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &___readMap; + std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; Json jOpt; if (option) { @@ -208,7 +208,7 @@ int ContextMonitor::__findSub(RequestType type, const char* subject, Json* optio bool ContextMonitor::__addSub(RequestType type, int sid, const char* subject, Json* option, IContextListener* listener) { - std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &___readMap; + std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; SubscrInfo *info = new(std::nothrow) SubscrInfo(sid, subject, option); IF_FAIL_RETURN_TAG(info, false, _E, "Memory allocation failed"); @@ -220,7 +220,7 @@ bool ContextMonitor::__addSub(RequestType type, int sid, const char* subject, Js void ContextMonitor::__removeSub(RequestType type, const char* subject, Json* option) { - std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &___readMap; + std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; Json jOpt; if (option) { @@ -238,7 +238,7 @@ void ContextMonitor::__removeSub(RequestType type, const char* subject, Json* op void ContextMonitor::__removeSub(RequestType type, int sid) { - std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &___readMap; + std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; SubscrInfo* info = map->at(sid); info->listenerList.clear(); @@ -252,7 +252,7 @@ void ContextMonitor::__removeSub(RequestType type, int sid) int ContextMonitor::__addListener(RequestType type, int sid, IContextListener* listener) { // @return number of listeners for the corresponding sid - std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &___readMap; + std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; auto it = map->find(sid); @@ -265,7 +265,7 @@ int ContextMonitor::__addListener(RequestType type, int sid, IContextListener* l int ContextMonitor::__removeListener(RequestType type, int sid, IContextListener* listener) { // @return number of listeners for the corresponding sid - std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &___readMap; + std::map* map = (type == REQ_SUBSCRIBE)? &__subscrMap : &__readMap; auto it = map->find(sid); @@ -293,8 +293,8 @@ void ContextMonitor::replyResult(int reqId, int error, const char* subject, Json { _D(YELLOW("Condition received: subject(%s), option(%s), fact(%s)"), subject, option->str().c_str(), fact->str().c_str()); - auto it = ___readMap.find(reqId); - IF_FAIL_VOID_TAG(it != ___readMap.end(), _E, "Request id not found"); + auto it = __readMap.find(reqId); + IF_FAIL_VOID_TAG(it != __readMap.end(), _E, "Request id not found"); SubscrInfo* info = it->second; for (auto it2 = info->listenerList.begin(); it2 != info->listenerList.end(); ++it2) { diff --git a/src/trigger/ContextMonitor.h b/src/trigger/ContextMonitor.h index d3c6966..473cac2 100644 --- a/src/trigger/ContextMonitor.h +++ b/src/trigger/ContextMonitor.h @@ -34,7 +34,7 @@ namespace trigger { class ContextMonitor { public: static ContextMonitor* getInstance(); - static void setContextManager(ContextManager* ctx_mgr); + static void setContextManager(ContextManager* ctxMgr); static void destroy(); int subscribe(int ruleId, std::string subject, Json option, IContextListener* listener); @@ -75,7 +75,7 @@ namespace trigger { }; std::map __subscrMap; - std::map ___readMap; + std::map __readMap; int __findSub(RequestType type, const char *subject, Json *option); bool __addSub(RequestType type, int sid, const char *subject, Json *option, IContextListener* listener); diff --git a/src/trigger/RuleManager.cpp b/src/trigger/RuleManager.cpp index 75a5a1d..afb8107 100644 --- a/src/trigger/RuleManager.cpp +++ b/src/trigger/RuleManager.cpp @@ -53,6 +53,19 @@ RuleManager::RuleManager() RuleManager::~RuleManager() { + // Release rule instances + _D("Release rule instances"); + for (auto it = __ruleMap.begin(); it != __ruleMap.end(); ++it) { + Rule* rule = static_cast(it->second); + + int error = rule->stop(); + if (error != ERR_NONE) { + _E("Failed to stop rule%d", it->first); + } + + delete rule; + } + __ruleMap.clear(); } bool RuleManager::init() diff --git a/src/trigger/Trigger.cpp b/src/trigger/Trigger.cpp index ac56d3c..244ac1f 100644 --- a/src/trigger/Trigger.cpp +++ b/src/trigger/Trigger.cpp @@ -97,6 +97,8 @@ void Trigger::__processRequest(RequestInfo* request) } else { _E("Invalid request"); } + + delete request; } void Trigger::__processInitialize(ContextManager* mgr) -- 2.7.4