[NUI][Scene3D] Fix memory leak issue for Model's Animation
authorEunki Hong <eunkiki.hong@samsung.com>
Sat, 28 Jan 2023 14:09:09 +0000 (23:09 +0900)
committerJaehyun Cho <jaehyun0cho@gmail.com>
Wed, 1 Feb 2023 07:07:33 +0000 (16:07 +0900)
Since we create new handle at csharp binder, we should call DeleteHandle function.

Note : Extensions.GetInstanceSafely delete the getted cPtr's reference inside of
that function. So we cannot use it.

Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
src/Tizen.NUI.Scene3D/src/public/Controls/Model.cs
src/Tizen.NUI.Scene3D/src/public/Controls/SceneView.cs

index 2ff3a0b..c270410 100755 (executable)
@@ -181,7 +181,20 @@ namespace Tizen.NUI.Scene3D
         /// <since_tizen> 10 </since_tizen>
         public Animation GetAnimation(uint index)
         {
-            Animation ret = new Animation(Interop.Model.GetAnimation(SwigCPtr, index), false);
+            global::System.IntPtr cPtr = Interop.Model.GetAnimation(SwigCPtr, index);
+            Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation;
+            if(ret == null)
+            {
+                // Register new animation into Registry.
+                ret = new Animation(cPtr, true);
+            }
+            else
+            {
+                // We found matched NUI animation. Reduce cPtr reference count.
+                HandleRef handle = new HandleRef(this, cPtr);
+                Tizen.NUI.Interop.Animation.DeleteAnimation(handle);
+                handle = new HandleRef(null, IntPtr.Zero);
+            }
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
             return ret;
         }
@@ -195,7 +208,20 @@ namespace Tizen.NUI.Scene3D
         /// <since_tizen> 10 </since_tizen>
         public Animation GetAnimation(string name)
         {
-            Animation ret = new Animation(Interop.Model.GetAnimation(SwigCPtr, name), false);
+            global::System.IntPtr cPtr = Interop.Model.GetAnimation(SwigCPtr, name);
+            Animation ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animation;
+            if(ret == null)
+            {
+                // Register new animation into Registry.
+                ret = new Animation(cPtr, true);
+            }
+            else
+            {
+                // We found matched NUI animation. Reduce cPtr reference count.
+                HandleRef handle = new HandleRef(this, cPtr);
+                Tizen.NUI.Interop.Animation.DeleteAnimation(handle);
+                handle = new HandleRef(null, IntPtr.Zero);
+            }
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
             return ret;
         }
@@ -206,9 +232,22 @@ namespace Tizen.NUI.Scene3D
         /// <returns>Root View of the model.</returns>
         // This will be public opened after ACR done. (Before ACR, need to be hidden as Inhouse API)
         [EditorBrowsable(EditorBrowsableState.Never)]
-        private View GetModelRoot()
+        private Animatable GetModelRoot()
         {
-            View ret = new View(Interop.Model.GetModelRoot(SwigCPtr), false);
+            global::System.IntPtr cPtr = Interop.Model.GetModelRoot(SwigCPtr);
+            Animatable ret = Registry.GetManagedBaseHandleFromNativePtr(cPtr) as Animatable;
+            if(ret == null)
+            {
+                // Register new animatable into Registry.
+                ret = new Animatable(cPtr, true);
+            }
+            else
+            {
+                // We found matched NUI animatable. Reduce cPtr reference count.
+                HandleRef handle = new HandleRef(this, cPtr);
+                Tizen.NUI.Interop.BaseHandle.DeleteBaseHandle(handle);
+                handle = new HandleRef(null, IntPtr.Zero);
+            }
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
             return ret;
         }
index 3042f6c..06302e9 100755 (executable)
@@ -533,7 +533,7 @@ namespace Tizen.NUI.Scene3D
         private Rotation GetSkyboxOrientation()
         {
             global::System.IntPtr cPtr = Interop.SceneView.GetSkyboxOrientation(SwigCPtr);
-            Rotation ret = (cPtr == global::System.IntPtr.Zero) ? null : new Rotation(cPtr, false);
+            Rotation ret = (cPtr == global::System.IntPtr.Zero) ? null : new Rotation(cPtr, true);
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
             return ret;
         }