(Rive) Make rasterize callback as unique_ptr 65/296465/3
authorEunki, Hong <eunkiki.hong@samsung.com>
Fri, 28 Jul 2023 06:39:37 +0000 (15:39 +0900)
committerEunki, Hong <eunkiki.hong@samsung.com>
Fri, 28 Jul 2023 06:47:07 +0000 (15:47 +0900)
Since UnregisterEventCallback don't delete the CallbackBase,
there might be some memory leak.

Similar patch applied on lottie. So let we also make it rive-animation-manager.

Change-Id: I386b52cf84cebbb4f4bfcf59cd00396d261a0bc0
Signed-off-by: Eunki, Hong <eunkiki.hong@samsung.com>
dali-extension/internal/rive-animation-view/rive-animation-manager.cpp
dali-extension/internal/rive-animation-view/rive-animation-manager.h
dali-extension/internal/rive-animation-view/rive-animation-view-impl.cpp

index 545e8aa..cbf5c99 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -40,7 +40,7 @@ Debug::Filter* gRiveAnimationLogFilter = Debug::Filter::New(Debug::NoLogging, fa
 } // unnamed namespace
 
 std::unique_ptr<RiveAnimationManager> RiveAnimationManager::mInstance = nullptr;
-std::once_flag RiveAnimationManager::mOnceFlag;
+std::once_flag                        RiveAnimationManager::mOnceFlag;
 
 RiveAnimationManager& RiveAnimationManager::GetInstance()
 {
@@ -60,10 +60,6 @@ RiveAnimationManager::RiveAnimationManager()
 
 RiveAnimationManager::~RiveAnimationManager()
 {
-  for(auto&& iter : mEventCallbacks)
-  {
-    delete iter;
-  }
   mEventCallbacks.clear();
 
   if(mProcessorRegistered)
@@ -104,7 +100,7 @@ RiveAnimationThread& RiveAnimationManager::GetRiveAnimationThread()
 
 void RiveAnimationManager::RegisterEventCallback(CallbackBase* callback)
 {
-  mEventCallbacks.push_back(callback);
+  mEventCallbacks.emplace_back(std::unique_ptr<Dali::CallbackBase>(callback));
 
   if(!mProcessorRegistered)
   {
@@ -115,7 +111,11 @@ void RiveAnimationManager::RegisterEventCallback(CallbackBase* callback)
 
 void RiveAnimationManager::UnregisterEventCallback(CallbackBase* callback)
 {
-  auto iter = std::find(mEventCallbacks.begin(), mEventCallbacks.end(), callback);
+  auto iter = std::find_if(mEventCallbacks.begin(),
+                           mEventCallbacks.end(),
+                           [callback](const std::unique_ptr<CallbackBase>& element) {
+                             return element.get() == callback;
+                           });
   if(iter != mEventCallbacks.end())
   {
     mEventCallbacks.erase(iter);
@@ -136,7 +136,6 @@ void RiveAnimationManager::Process(bool postProcessor)
   for(auto&& iter : mEventCallbacks)
   {
     CallbackBase::Execute(*iter);
-    delete iter;
   }
   mEventCallbacks.clear();
 
@@ -146,6 +145,6 @@ void RiveAnimationManager::Process(bool postProcessor)
 
 } // namespace Internal
 
-} // namespace Toolkit
+} // namespace Extension
 
 } // namespace Dali
index b5a4811..ba08c60 100644 (file)
@@ -2,7 +2,7 @@
 #define DALI_EXTENSION_INTERNAL_RIVE_ANIMATION_MANAGER_H
 
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -110,10 +110,10 @@ private:
   static std::unique_ptr<RiveAnimationManager> mInstance;
   static std::once_flag                        mOnceFlag;
 
-  std::vector<CallbackBase*>           mEventCallbacks;
-  std::vector<LifecycleObserver*>      mLifecycleObservers;
-  std::unique_ptr<RiveAnimationThread> mRiveAnimationThread;
-  bool                                 mProcessorRegistered;
+  std::vector<std::unique_ptr<CallbackBase>> mEventCallbacks;
+  std::vector<LifecycleObserver*>            mLifecycleObservers;
+  std::unique_ptr<RiveAnimationThread>       mRiveAnimationThread;
+  bool                                       mProcessorRegistered;
 };
 
 } // namespace Internal
index 4df35eb..83c5f89 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -124,6 +124,7 @@ RiveAnimationView::~RiveAnimationView()
     if(mEventCallback)
     {
       riveAnimationManager.UnregisterEventCallback(mEventCallback);
+      mEventCallback = nullptr;
     }
 
     // Finalize animation task and disconnect the signal in the main thread