DisConnect from Signals via using BaseHandleCPtr 43/144843/7
authorMuhammad Umar <m.umar@partner.samsung.com>
Fri, 18 Aug 2017 06:29:09 +0000 (15:29 +0900)
committerAdeel Kazmi <adeel.kazmi@samsung.com>
Mon, 25 Sep 2017 15:24:07 +0000 (15:24 +0000)
There was a crash issue when disconnecting from signals from any class derived from View. This was due to the fact that the handle has been deleted earlier in derived class dispose function.

Change-Id: Ib5c383b82dbd5ddab5010acdb82048fb27737df5

Tizen.NUI/src/public/BaseComponents/Scrollable.cs
Tizen.NUI/src/public/BaseComponents/View.cs
Tizen.NUI/src/public/BaseHandle.cs
Tizen.NUI/src/public/UIComponents/Button.cs

index 37f26bb..3803986 100755 (executable)
@@ -53,6 +53,29 @@ namespace Tizen.NUI.BaseComponents
             //You should not access any managed member here except static instance.
             //because the execution order of Finalizes is non-deterministic.
 
+            DisConnectFromSignals();
+
+            if (swigCPtr.Handle != global::System.IntPtr.Zero)
+            {
+                if (swigCMemOwn)
+                {
+                    swigCMemOwn = false;
+                    NDalicPINVOKE.delete_Scrollable(swigCPtr);
+                }
+                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
+            }
+
+            base.Dispose(type);
+        }
+
+        private void DisConnectFromSignals()
+        {
+            // Save current CPtr.
+            global::System.Runtime.InteropServices.HandleRef currentCPtr = swigCPtr;
+
+            // Use BaseHandle CPtr as current might have been deleted already in derived classes.
+            swigCPtr = GetBaseHandleCPtrHandleRef;
+
             if (_scrollableCompletedCallbackDelegate != null)
             {
                 this.ScrollCompletedSignal().Disconnect(_scrollableCompletedCallbackDelegate);
@@ -68,17 +91,9 @@ namespace Tizen.NUI.BaseComponents
                 this.ScrollStartedSignal().Disconnect(_scrollableStartedCallbackDelegate);
             }
 
-            if (swigCPtr.Handle != global::System.IntPtr.Zero)
-            {
-                if (swigCMemOwn)
-                {
-                    swigCMemOwn = false;
-                    NDalicPINVOKE.delete_Scrollable(swigCPtr);
-                }
-                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
-            }
-
-            base.Dispose(type);
+            // BaseHandle CPtr is used in Registry and there is danger of deletion if we keep using it here.
+            // Restore current CPtr.
+            swigCPtr = currentCPtr;
         }
 
         public class StartedEventArgs : EventArgs
index 0b322a8..ff714c0 100755 (executable)
@@ -140,6 +140,29 @@ namespace Tizen.NUI.BaseComponents
             //You should not access any managed member here except static instance.
             //because the execution order of Finalizes is non-deterministic.
 
+            DisConnectFromSignals();
+
+            if (swigCPtr.Handle != global::System.IntPtr.Zero)
+            {
+                if (swigCMemOwn)
+                {
+                    swigCMemOwn = false;
+                    NDalicPINVOKE.delete_View(swigCPtr);
+                }
+                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
+            }
+
+            base.Dispose(type);
+        }
+
+        private void DisConnectFromSignals()
+        {
+            // Save current CPtr.
+            global::System.Runtime.InteropServices.HandleRef currentCPtr = swigCPtr;
+
+            // Use BaseHandle CPtr as current might have been deleted already in derived classes.
+            swigCPtr = GetBaseHandleCPtrHandleRef;
+
             if (_onRelayoutEventCallback != null)
             {
                 this.OnRelayoutSignal().Disconnect(_onRelayoutEventCallback);
@@ -220,17 +243,9 @@ namespace Tizen.NUI.BaseComponents
                 this.KeyInputFocusGainedSignal().Disconnect(_keyInputFocusGainedCallback);
             }
 
-            if (swigCPtr.Handle != global::System.IntPtr.Zero)
-            {
-                if (swigCMemOwn)
-                {
-                    swigCMemOwn = false;
-                    NDalicPINVOKE.delete_View(swigCPtr);
-                }
-                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
-            }
-
-            base.Dispose(type);
+            // BaseHandle CPtr is used in Registry and there is danger of deletion if we keep using it here.
+            // Restore current CPtr.
+            swigCPtr = currentCPtr;
         }
 
         private EventHandler _keyInputFocusGainedEventHandler;
index 130d455..6ff87e9 100755 (executable)
@@ -329,6 +329,14 @@ namespace Tizen.NUI
             return ret;
         }
 
+        internal global::System.Runtime.InteropServices.HandleRef GetBaseHandleCPtrHandleRef
+        {
+            get
+            {
+                return swigCPtr;
+            }
+        }
+
     }
 
 }
index 25b6182..43eb91a 100755 (executable)
@@ -72,6 +72,29 @@ namespace Tizen.NUI.UIComponents
             //You should not access any managed member here except static instance.
             //because the execution order of Finalizes is non-deterministic.
 
+            DisConnectFromSignals();
+
+            if (swigCPtr.Handle != global::System.IntPtr.Zero)
+            {
+                if (swigCMemOwn)
+                {
+                    swigCMemOwn = false;
+                    NDalicPINVOKE.delete_Button(swigCPtr);
+                }
+                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
+            }
+
+            base.Dispose(type);
+        }
+
+        private void DisConnectFromSignals()
+        {
+            // Save current CPtr.
+            global::System.Runtime.InteropServices.HandleRef currentCPtr = swigCPtr;
+
+            // Use BaseHandle CPtr as current might have been deleted already in derived classes.
+            swigCPtr = GetBaseHandleCPtrHandleRef;
+
             if (_stateChangedCallback != null)
             {
                 StateChangedSignal().Disconnect(_stateChangedCallback);
@@ -92,17 +115,9 @@ namespace Tizen.NUI.UIComponents
                 ClickedSignal().Disconnect(_clickedCallback);
             }
 
-            if (swigCPtr.Handle != global::System.IntPtr.Zero)
-            {
-                if (swigCMemOwn)
-                {
-                    swigCMemOwn = false;
-                    NDalicPINVOKE.delete_Button(swigCPtr);
-                }
-                swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
-            }
-
-            base.Dispose(type);
+            // BaseHandle CPtr is used in Registry and there is danger of deletion if we keep using it here.
+            // Restore current CPtr.
+            swigCPtr = currentCPtr;
         }