[NUI] BaseHandle without Registry but own memory
authorEunki Hong <eunkiki.hong@samsung.com>
Sun, 8 Oct 2023 07:02:42 +0000 (16:02 +0900)
committerEunki Hong <h.pichulia@gmail.com>
Wed, 25 Oct 2023 12:48:44 +0000 (21:48 +0900)
There are some usecase s.t. we have to call ReleaseSwigCPtr
but don't want register to Registry.

For example, PixelBuffer and PixelData dont need to be create
only for main thread.

Signed-off-by: Eunki Hong <eunkiki.hong@samsung.com>
src/Tizen.NUI/src/public/Common/BaseHandle.cs
src/Tizen.NUI/src/public/Events/Hover.cs
src/Tizen.NUI/src/public/Events/Touch.cs
src/Tizen.NUI/src/public/Events/Wheel.cs
src/Tizen.NUI/src/public/Images/PixelBuffer.cs
src/Tizen.NUI/src/public/Images/PixelData.cs
src/Tizen.NUI/src/public/Input/Key.cs
src/Tizen.NUI/src/public/Window/Window.cs

index 3b2bfafe88aecb06ea4a2d95fce6dff565ff0c95..5f85509181bd69bf7ce7a82a68baffbca3379074 100755 (executable)
@@ -69,6 +69,29 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
+        internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister)
+        {
+            //to catch derived classes dali native exceptions
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            DebugFileLogging.Instance.WriteLog($"BaseHandle.contructor with cMemeryOwn:{cMemoryOwn} and cRegister:{cRegister} START");
+
+            registerMe = cRegister;
+            swigCMemOwn = cMemoryOwn;
+            swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
+            if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
+
+            if (registerMe)
+            {
+                // Register this instance of BaseHandle in the registry.
+                Registry.Register(this);
+            }
+
+            disposeDebuggingCtor();
+            DebugFileLogging.Instance.WriteLog($" BaseHandle.contructor with cMemeryOwn and cRegister END");
+            DebugFileLogging.Instance.WriteLog($"=============================");
+        }
+
         internal BaseHandle(global::System.IntPtr cPtr, bool cMemoryOwn)
         {
             //to catch derived classes dali native exceptions
@@ -511,6 +534,15 @@ namespace Tizen.NUI
             PropertySet?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
 
+        internal void UnregisterFromRegistry()
+        {
+            if (registerMe)
+            {
+                Registry.Unregister(this);
+                registerMe = false;
+            }
+        }
+
         /// <summary>
         /// Dispose.
         /// </summary>
@@ -537,10 +569,7 @@ namespace Tizen.NUI
             //because the execution order of Finalizes is non-deterministic.
 
             //Unreference this instance from Registry.
-            if (registerMe)
-            {
-                Registry.Unregister(this);
-            }
+            UnregisterFromRegistry();
 
             disposeDebuggingDispose(type);
 
index 20f0e6d188301405b5d61b610c3d9ae1cf88352e..a35082684d6392e0132c2bb5208da9c67e25be53 100755 (executable)
@@ -52,7 +52,11 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
-        internal Hover(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        internal Hover(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, true)
+        {
+        }
+
+        internal Hover(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
         {
         }
 
index 7fb042090dd42b93a31ee952b456eb8d30564d0a..ee147e960d213e942e9a9b5dacba7d2a25325e26 100755 (executable)
@@ -42,7 +42,11 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
-        internal Touch(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        internal Touch(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, true)
+        {
+        }
+
+        internal Touch(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
         {
         }
 
index 368da455ae17224c4debb6b019037f18f2301a95..00ef79d95b7e145c0ee0cff5ddf1f1eda8482969 100755 (executable)
@@ -53,7 +53,11 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
-        internal Wheel(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        internal Wheel(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, true)
+        {
+        }
+
+        internal Wheel(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
         {
         }
 
index efb90df19741832751cbf9e332c697b57167576e..79c74d0929638bdaa0e01183886313c1a38f883b 100755 (executable)
@@ -56,7 +56,12 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
-        internal PixelBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        internal PixelBuffer(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, false)
+        {
+            // Note : PixelBuffer don't need to be register in Registry default. So we can create this class from worker thread.
+        }
+
+        internal PixelBuffer(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
         {
         }
 
index b488f5ea7abcb462b2900b792131ef33ce9e5d4c..9f915ad4b2a3bf79f5fb20cd8a246d222f1ca47c 100755 (executable)
@@ -72,7 +72,12 @@ namespace Tizen.NUI
 
         }
 
-        internal PixelData(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        internal PixelData(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, false)
+        {
+            // Note : PixelData don't need to be register in Registry default. So we can create this class from worker thread.
+        }
+
+        internal PixelData(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
         {
         }
 
index 2ce31b9056e100fe643d2cab9107a9cbe8f70f7c..814b9b266c64e07167c7093b3962fd44564a954d 100755 (executable)
@@ -49,7 +49,11 @@ namespace Tizen.NUI
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
-        internal Key(global::System.IntPtr cPtr, bool cMemoryOwn) : base(cPtr, cMemoryOwn)
+        internal Key(global::System.IntPtr cPtr, bool cMemoryOwn) : this(cPtr, cMemoryOwn, true)
+        {
+        }
+
+        internal Key(global::System.IntPtr cPtr, bool cMemoryOwn, bool cRegister) : base(cPtr, cMemoryOwn, cRegister)
         {
         }
 
index e25d45e8a6be0a8ce05463b9f5554c7d42d62257..36d60001bbe9c7a4981f9e199ebb25c200831de2 100755 (executable)
@@ -2112,22 +2112,14 @@ namespace Tizen.NUI
         /// We will use weak reference of last key events.
         /// Return value will be invalidated if last key event changed internally.
         /// </remarks>
-        /// <remarks>
-        /// Do not Dispose this value.
-        /// </remarks>
         /// <returns>The last key event the window gets.</returns>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public Key GetLastKeyEvent()
         {
             if (internalLastKeyEvent == null)
             {
-                // TODO : We need to make automatically release memory of these cached events in future.
-                if (!(internalLastKeyEvent?.IsNativeHandleInvalid() ?? true))
-                {
-                    Interop.Key.DeleteKey(internalLastKeyEvent.SwigCPtr);
-                }
                 // Create empty event handle without register.
-                internalLastKeyEvent = new Key(Interop.Key.New(), false);
+                internalLastKeyEvent = new Key(Interop.Key.New(), true, false);
             }
             Interop.Window.InternalRetrievingLastKeyEvent(SwigCPtr, internalLastKeyEvent.SwigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2141,22 +2133,14 @@ namespace Tizen.NUI
         /// We will use weak reference of last touch events.
         /// Return value will be invalidated if last touch event changed internally.
         /// </remarks>
-        /// <remarks>
-        /// Do not Dispose this value.
-        /// </remarks>
         /// <returns>The last touch event the window gets.</returns>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public Touch GetLastTouchEvent()
         {
             if (internalLastTouchEvent == null)
             {
-                // TODO : We need to make automatically release memory of these cached events in future.
-                if (!(internalLastTouchEvent?.IsNativeHandleInvalid() ?? true))
-                {
-                    Interop.Touch.DeleteTouch(internalLastTouchEvent.SwigCPtr);
-                }
                 // Create empty event handle without register.
-                internalLastTouchEvent = new Touch(Interop.Touch.NewTouch(), false);
+                internalLastTouchEvent = new Touch(Interop.Touch.NewTouch(), true, false);
             }
             Interop.Window.InternalRetrievingLastTouchEvent(SwigCPtr, internalLastTouchEvent.SwigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2170,22 +2154,14 @@ namespace Tizen.NUI
         /// We will use weak reference of last hover events.
         /// Return value will be invalidated if last hover event changed internally.
         /// </remarks>
-        /// <remarks>
-        /// Do not Dispose this value.
-        /// </remarks>
         /// <returns>The last hover event the window gets.</returns>
         [EditorBrowsable(EditorBrowsableState.Never)]
         public Hover GetLastHoverEvent()
         {
             if (internalLastHoverEvent == null)
             {
-                // TODO : We need to make automatically release memory of these cached events in future.
-                if (!(internalLastHoverEvent?.IsNativeHandleInvalid() ?? true))
-                {
-                    Interop.Hover.DeleteHover(internalLastHoverEvent.SwigCPtr);
-                }
                 // Create empty event handle without register.
-                internalLastHoverEvent = new Hover(Interop.Hover.New(0u), false);
+                internalLastHoverEvent = new Hover(Interop.Hover.New(0u), true, false);
             }
             Interop.Window.InternalRetrievingLastHoverEvent(SwigCPtr, internalLastHoverEvent.SwigCPtr);
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
@@ -2274,20 +2250,6 @@ namespace Tizen.NUI
 
                 localController?.Dispose();
 
-                // TODO : We need to make automatically release memory of these cached events in future.
-                if (!(internalLastKeyEvent?.IsNativeHandleInvalid() ?? true))
-                {
-                    Interop.Key.DeleteKey(internalLastKeyEvent.SwigCPtr);
-                }
-                if (!(internalLastTouchEvent?.IsNativeHandleInvalid() ?? true))
-                {
-                    Interop.Touch.DeleteTouch(internalLastTouchEvent.SwigCPtr);
-                }
-                if (!(internalLastHoverEvent?.IsNativeHandleInvalid() ?? true))
-                {
-                    Interop.Hover.DeleteHover(internalLastHoverEvent.SwigCPtr);
-                }
-
                 internalLastKeyEvent?.Dispose();
                 internalLastKeyEvent = null;
                 internalLastTouchEvent?.Dispose();