Create another native handle to make sure Registry.Unregister works fine.
authorUmar <m.umar@partner.samsung.com>
Wed, 12 Jul 2017 14:43:57 +0000 (15:43 +0100)
committerUmar <m.umar@partner.samsung.com>
Tue, 18 Jul 2017 13:20:33 +0000 (14:20 +0100)
This is much simpler solution with less code maintenance as derived classes do not have to call Registry.Unregister(this);.
Only BaseHandle class will be responsible for Registry.Register and Registry.UnRegister which is much simpler and symmetric solution.
The only drawback is that we have to create an extra native handle.

Change-Id: I2e3ab7b3311befb25141ae0816b7d2d77a2e3485

src/Tizen.NUI/src/public/BaseHandle.cs

index 8ac41e0..d14f943 100755 (executable)
@@ -27,6 +27,18 @@ namespace Tizen.NUI
             swigCMemOwn = cMemoryOwn;
             swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
 
+            // using copy constructor to create another native handle so Registry.Unregister works fine.
+            swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, NDalicPINVOKE.new_BaseHandle__SWIG_2(swigCPtr));
+
+            // Register this instance of BaseHandle in the registry.
+            Registry.Register(this);
+        }
+
+        internal BaseHandle(global::System.IntPtr cPtr)
+        {
+            swigCMemOwn = true;
+            swigCPtr = new global::System.Runtime.InteropServices.HandleRef(this, cPtr);
+
             // Register this instance of BaseHandle in the registry.
             Registry.Register(this);
         }
@@ -93,11 +105,8 @@ namespace Tizen.NUI
 
             if (swigCPtr.Handle != global::System.IntPtr.Zero)
             {
-                if (swigCMemOwn)
-                {
-                    swigCMemOwn = false;
-                    NDalicPINVOKE.delete_BaseHandle(swigCPtr);
-                }
+                swigCMemOwn = false;
+                NDalicPINVOKE.delete_BaseHandle(swigCPtr);
                 swigCPtr = new global::System.Runtime.InteropServices.HandleRef(null, global::System.IntPtr.Zero);
             }
 
@@ -228,12 +237,12 @@ namespace Tizen.NUI
         }
 
 
-        public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1(), true)
+        public BaseHandle() : this(NDalicPINVOKE.new_BaseHandle__SWIG_1())
         {
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }
 
-        public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)), true)
+        public BaseHandle(BaseHandle handle) : this(NDalicPINVOKE.new_BaseHandle__SWIG_2(BaseHandle.getCPtr(handle)))
         {
             if (NDalicPINVOKE.SWIGPendingException.Pending) throw NDalicPINVOKE.SWIGPendingException.Retrieve();
         }