Remove event after using to prevent leak
authorSukhyungKang <shine.kang@samsung.com>
Wed, 20 Dec 2023 09:12:11 +0000 (18:12 +0900)
committerSukhyungKang <shine.kang@samsung.com>
Thu, 28 Dec 2023 06:54:23 +0000 (15:54 +0900)
Change-Id: Id9cdaf2dfd4fce11ff280e29ddeeda03f84b061a
Signed-off-by: SukhyungKang <shine.kang@samsung.com>
src/service_common.cc

index 4983cce..795c1de 100644 (file)
@@ -98,17 +98,32 @@ void PackageEventListener::OnPkgmgrEvent(std::shared_ptr<PkgmgrEventArgs> args)
   }
 
   if (args->GetEventName().compare(std::string("ok")) == 0) {
-    for (auto& a : pkgmgr_event_list_) {
-      if (a->GetTargetUid() == args->GetTargetUid() &&
-          a->GetPkgId().compare(args->GetPkgId()) == 0) {
-        if (a->GetEventName().compare(std::string("install")) == 0)
+    auto iter = pkgmgr_event_list_.begin();
+    while(iter != pkgmgr_event_list_.end()) {
+      if ((*iter)->GetTargetUid() == args->GetTargetUid() &&
+          (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) {
+        if ((*iter)->GetEventName().compare(std::string("install")) == 0)
           _package_install_cb(args->GetTargetUid(), args->GetPkgId().c_str());
-        else if (a->GetEventName().compare(std::string("uninstall")) == 0)
+        else if ((*iter)->GetEventName().compare(std::string("uninstall")) == 0)
           _package_uninstall_cb(args->GetTargetUid(), args->GetPkgId().c_str());
-        else if (a->GetEventName().compare(std::string("enable_app")) == 0)
+        else if ((*iter)->GetEventName().compare(std::string("enable_app")) == 0)
           _app_enabled_cb(args->GetTargetUid(), args->GetPkgId().c_str());
-        else if (a->GetEventName().compare(std::string("disable_app")) == 0)
+        else if ((*iter)->GetEventName().compare(std::string("disable_app")) == 0)
           _app_disabled_cb(args->GetTargetUid(), args->GetPkgId().c_str());
+
+        iter = pkgmgr_event_list_.erase(iter);
+      } else {
+        iter++;
+      }
+    }
+  } else if (args->GetEventName().compare(std::string("fail")) == 0) {
+    auto iter = pkgmgr_event_list_.begin();
+    while(iter != pkgmgr_event_list_.end()) {
+      if ((*iter)->GetTargetUid() == args->GetTargetUid() &&
+          (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) {
+        iter = pkgmgr_event_list_.erase(iter);
+      } else {
+        iter++;
       }
     }
   }
@@ -127,17 +142,32 @@ void PackageEventListener::OnPkgmgrAppEvent(std::shared_ptr<PkgmgrAppEventArgs>
   }
 
   if (args->GetEventName().compare(std::string("ok")) == 0) {
-    for (auto& a : pkgmgr_app_event_list_) {
-      if (a->GetTargetUid() == args->GetTargetUid() &&
-          a->GetPkgId().compare(args->GetPkgId()) == 0) {
-        if (a->GetEventName().compare(std::string("install")) == 0)
+    auto iter = pkgmgr_app_event_list_.begin();
+    while(iter != pkgmgr_app_event_list_.end()) {
+      if ((*iter)->GetTargetUid() == args->GetTargetUid() &&
+          (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) {
+        if ((*iter)->GetEventName().compare(std::string("install")) == 0)
           _package_install_cb(args->GetTargetUid(), args->GetPkgId().c_str());
-        else if (a->GetEventName().compare(std::string("uninstall")) == 0)
+        else if ((*iter)->GetEventName().compare(std::string("uninstall")) == 0)
           _package_uninstall_cb(args->GetTargetUid(), args->GetPkgId().c_str());
-        else if (a->GetEventName().compare(std::string("enable_app")) == 0)
+        else if ((*iter)->GetEventName().compare(std::string("enable_app")) == 0)
           _app_enabled_cb(args->GetTargetUid(), args->GetPkgId().c_str());
-        else if (a->GetEventName().compare(std::string("disable_app")) == 0)
+        else if ((*iter)->GetEventName().compare(std::string("disable_app")) == 0)
           _app_disabled_cb(args->GetTargetUid(), args->GetPkgId().c_str());
+
+        iter = pkgmgr_app_event_list_.erase(iter);
+      } else {
+        iter++;
+      }
+    }
+  } else if (args->GetEventName().compare(std::string("fail")) == 0) {
+    auto iter = pkgmgr_app_event_list_.begin();
+    while(iter != pkgmgr_app_event_list_.end()) {
+      if ((*iter)->GetTargetUid() == args->GetTargetUid() &&
+          (*iter)->GetPkgId().compare(args->GetPkgId()) == 0) {
+        iter = pkgmgr_app_event_list_.erase(iter);
+      } else {
+        iter++;
       }
     }
   }