[Information] Fix delegate lifetime issue (#52)
authorkichankwon <35328573+kichankwon@users.noreply.github.com>
Thu, 18 Jan 2018 10:01:31 +0000 (19:01 +0900)
committerChungryeol Lim <darkleem@naver.com>
Thu, 18 Jan 2018 10:01:31 +0000 (19:01 +0900)
Signed-off-by: kichankwon <k_c.kwon@samsung.com>
src/Tizen.System.Information/RuntimeInfo/RuntimeInfoEventHandler.cs

index 011073a..8dde55d 100755 (executable)
@@ -29,13 +29,33 @@ namespace Tizen.System
             Handler = null;
         }
 
+        private static Interop.RuntimeInfo.RuntimeInformationChangedCallback __callback;
+
         internal event EventHandler<RuntimeFeatureStatusChangedEventArgs> EventHandler
         {
             add
             {
                 if (Handler == null)
                 {
-                    InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(Key), RuntimeInformationChangedCallback, IntPtr.Zero);
+                    __callback = (RuntimeInfoKey num, IntPtr userData) =>
+                    {
+                        string strKey = "Invalid";
+                        RuntimeInfoKey key = TvProductHelper.ReconvertKeyIfTvProduct(num);
+
+                        if (key > 0 && Information.EnumStringMapping.ContainsKey(key))
+                        {
+                            strKey = Information.EnumStringMapping[key];
+                        }
+
+                        RuntimeFeatureStatusChangedEventArgs eventArgs = new RuntimeFeatureStatusChangedEventArgs()
+                        {
+                            Key = Information.HttpPrefix + Information.RuntimeInfoStringKeyPrefix + strKey
+                        };
+
+                        Handler?.Invoke(null, eventArgs);
+                    };
+
+                    InformationError ret = Interop.RuntimeInfo.SetRuntimeInfoChangedCallback(TvProductHelper.ConvertKeyIfTvProduct(Key), __callback, IntPtr.Zero);
                     if (ret != InformationError.None)
                     {
                         Log.Error(InformationErrorFactory.LogTag, "Interop failed to add event handler");
@@ -58,23 +78,5 @@ namespace Tizen.System
                 }
             }
         }
-
-        private void RuntimeInformationChangedCallback(RuntimeInfoKey num, IntPtr userData)
-        {
-            string strKey = "Invalid";
-            RuntimeInfoKey key = TvProductHelper.ReconvertKeyIfTvProduct(num);
-            
-            if (key > 0 && Information.EnumStringMapping.ContainsKey(key))
-            {
-                strKey = Information.EnumStringMapping[key];
-            }
-
-            RuntimeFeatureStatusChangedEventArgs eventArgs = new RuntimeFeatureStatusChangedEventArgs()
-            {
-                Key = Information.HttpPrefix + Information.RuntimeInfoStringKeyPrefix + strKey
-            };
-
-            Handler?.Invoke(null, eventArgs);
-        }
     }
 }