cordova-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From purplecabb...@apache.org
Subject [1/2] git commit: CB-7028 fixed memory leak in wp with plugins
Date Wed, 30 Jul 2014 18:41:27 GMT
Repository: cordova-wp8
Updated Branches:
  refs/heads/master 63749767c -> fb9cf558c


CB-7028 fixed memory leak in wp with plugins

When navigating between a native and hybrid page in wp8, if the
network-information or device plugins are installed the hybrid page will
never get garbage collected. Fixed by removing event handlers for commands
when the web browser is unloaded.


Project: http://git-wip-us.apache.org/repos/asf/cordova-wp8/repo
Commit: http://git-wip-us.apache.org/repos/asf/cordova-wp8/commit/28ed6f59
Tree: http://git-wip-us.apache.org/repos/asf/cordova-wp8/tree/28ed6f59
Diff: http://git-wip-us.apache.org/repos/asf/cordova-wp8/diff/28ed6f59

Branch: refs/heads/master
Commit: 28ed6f5922384661e0d111710dbc683cc1955efd
Parents: 9181154
Author: Staci Cooper <smcooper@us.ibm.com>
Authored: Thu Jul 3 16:12:35 2014 -0400
Committer: Staci Cooper <smcooper@us.ibm.com>
Committed: Thu Jul 3 16:12:35 2014 -0400

----------------------------------------------------------------------
 wp8/template/cordovalib/Commands/BaseCommand.cs | 10 +++++++++
 wp8/template/cordovalib/NativeExecution.cs      | 23 ++++++++++++++++++++
 2 files changed, 33 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/28ed6f59/wp8/template/cordovalib/Commands/BaseCommand.cs
----------------------------------------------------------------------
diff --git a/wp8/template/cordovalib/Commands/BaseCommand.cs b/wp8/template/cordovalib/Commands/BaseCommand.cs
index 9de0e4d..3e2e690 100644
--- a/wp8/template/cordovalib/Commands/BaseCommand.cs
+++ b/wp8/template/cordovalib/Commands/BaseCommand.cs
@@ -172,6 +172,16 @@ namespace WPCordovaClassLib.Cordova.Commands
             this.OnCommandResult = null;
         }
 
+        public void DetachHandlers()
+        {
+            this.OnCommandResult = null;
+            this.OnCustomScript = null;
+            foreach (string callbackId in new List<string>(ResultHandlers.Keys))
+            {
+                RemoveResultHandler(callbackId);
+            }
+        }
+
         public static string GetBaseURL()
         {
 #if CORDOVA_CLASSLIB

http://git-wip-us.apache.org/repos/asf/cordova-wp8/blob/28ed6f59/wp8/template/cordovalib/NativeExecution.cs
----------------------------------------------------------------------
diff --git a/wp8/template/cordovalib/NativeExecution.cs b/wp8/template/cordovalib/NativeExecution.cs
index e8bda8f..18ca910 100644
--- a/wp8/template/cordovalib/NativeExecution.cs
+++ b/wp8/template/cordovalib/NativeExecution.cs
@@ -18,6 +18,7 @@ using System.Threading;
 using Microsoft.Devices;
 using Microsoft.Phone.Controls;
 using WPCordovaClassLib.Cordova.Commands;
+using System.Collections.Generic;
 using System.Windows;
 
 namespace WPCordovaClassLib.Cordova
@@ -34,6 +35,11 @@ namespace WPCordovaClassLib.Cordova
         private readonly WebBrowser webBrowser;
 
         /// <summary>
+        /// List of commands with attached handlers
+        /// </summary>
+        private List<BaseCommand> commands;
+
+        /// <summary>
         /// Creates new instance of a NativeExecution class.
         /// </summary>
         /// <param name="browser">Reference to web part where application is hosted</param>
@@ -45,6 +51,22 @@ namespace WPCordovaClassLib.Cordova
             }
 
             this.webBrowser = browser;
+            this.commands = new List<BaseCommand>();
+            webBrowser.Unloaded += webBrowser_Unloaded;
+        }
+
+        /// <summary>
+        /// Detaches event handlers to prevent memory leak on page navigation
+        /// </summary>
+        void webBrowser_Unloaded(object sender, RoutedEventArgs e)
+        {
+            for (int i = commands.Count - 1; i >= 0; i--)
+            {
+                if (commands[i] != null)
+                {
+                    commands[i].DetachHandlers();
+                }
+            }
         }
 
         /// <summary>
@@ -120,6 +142,7 @@ namespace WPCordovaClassLib.Cordova
                     try
                     {
                         bc.InvokeMethodNamed(commandCallParams.CallbackId, commandCallParams.Action,
commandCallParams.Args);
+                        commands.Add(bc);
                     }
                     catch (Exception ex)
                     {


Mime
View raw message