[WidgetControl]Fix callback memory freed bug 79/161179/1 4.0.1-preview1-00027
authorHyunho Kang <hhstark.kang@samsung.com>
Wed, 15 Nov 2017 08:05:02 +0000 (17:05 +0900)
committerSemun Lee <semun.lee@samsung.com>
Wed, 22 Nov 2017 04:54:44 +0000 (04:54 +0000)
Change-Id: Ib380538352686ce1e4d50ebc810a28024913d6bf
Signed-off-by: Hyunho Kang <hhstark.kang@samsung.com>
(cherry picked from commit 92d4fa62d90b55e7b6d487c3c5aa764e1c224bf5)

src/Tizen.Applications.WidgetControl/Interop/Interop.WidgetService.cs
src/Tizen.Applications.WidgetControl/Tizen.Applications/WidgetControl.cs

index 995814c..3be41ed 100755 (executable)
@@ -56,7 +56,7 @@ internal static partial class Interop
         internal delegate void InstanceCallback(string widgetId, string instanceId, IntPtr userData);
 
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
-        internal delegate void LifecycleCallback(string widgetId, LifecycleEvent e, string instanceId, IntPtr userData);
+        internal delegate int LifecycleCallback(string widgetId, LifecycleEvent e, string instanceId, IntPtr userData);
 
         [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
         internal delegate void WidgetListCallback(string widgetId, int isPrime, IntPtr userData);
index ff6906a..fe4a3d5 100755 (executable)
@@ -28,6 +28,7 @@ namespace Tizen.Applications
     public class WidgetControl : IDisposable
     {
         private const string LogTag = "Tizen.Applications.WidgetControl";
+        private static Interop.WidgetService.LifecycleCallback _onLifecycleCallback;
         /// <summary>
         /// Class for the widget instance.
         /// </summary>
@@ -616,8 +617,10 @@ namespace Tizen.Applications
 
             if (s_lifecycleEventRefCnt[Id] == 0)
             {
-                Interop.WidgetService.ErrorCode err = Interop.WidgetService.SetLifecycleEvent(Id, OnLifecycleEvent, IntPtr.Zero);
+                if (_onLifecycleCallback == null)
+                    _onLifecycleCallback = new Interop.WidgetService.LifecycleCallback(OnLifecycleEvent);
 
+                Interop.WidgetService.ErrorCode err = Interop.WidgetService.SetLifecycleEvent(Id, _onLifecycleCallback, IntPtr.Zero);
                 switch (err)
                 {
                     case Interop.WidgetService.ErrorCode.InvalidParameter:
@@ -630,6 +633,7 @@ namespace Tizen.Applications
 
             s_lifecycleEventRefCnt[Id]++;
             s_eventObjects.Add(this);
+            Log.Debug(LogTag, "register lifecycle cb " + Id + " [" + s_lifecycleEventRefCnt[Id] + "]");
         }
 
         private void UnregisterLifecycleEvent()
@@ -658,14 +662,17 @@ namespace Tizen.Applications
                     case Interop.WidgetService.ErrorCode.NotExist:
                         throw new InvalidOperationException("Event handler is not exist");
                 }
+                _onLifecycleCallback = null;
             }
 
             s_eventObjects.Remove(this);
             s_lifecycleEventRefCnt[Id]--;
+            Log.Debug(LogTag, "unregister lifecycle cb " + Id + " [" + s_lifecycleEventRefCnt[Id] + "]");
         }
 
-        private static void OnLifecycleEvent(string widgetId, Interop.WidgetService.LifecycleEvent e, string instanceId, IntPtr userData)
+        private static int OnLifecycleEvent(string widgetId, Interop.WidgetService.LifecycleEvent e, string instanceId, IntPtr userData)
         {
+            Log.Debug(LogTag, "Lifecycle event : " + instanceId + " [" + e + "]");
             switch (e)
             {
                 case Interop.WidgetService.LifecycleEvent.Created:
@@ -728,6 +735,7 @@ namespace Tizen.Applications
                     }
                     break;
             }
+            return 0;
 
         }
     }