incubator-callback-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fil...@apache.org
Subject [15/28] git commit: Fixed a memory leak issue with WebWorks
Date Tue, 03 Jan 2012 20:22:59 GMT
Fixed a memory leak issue with WebWorks


Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-blackberry-webworks/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-cordova-blackberry-webworks/commit/571aed21
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-blackberry-webworks/tree/571aed21
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-blackberry-webworks/diff/571aed21

Branch: refs/heads/master
Commit: 571aed21e1f5cec9129908638c5cfbaefbffe924
Parents: 9b57534
Author: Fil Maj <filip.maj@nitobi.com>
Authored: Wed Nov 30 10:48:47 2011 -0800
Committer: Fil Maj <filip.maj@nitobi.com>
Committed: Wed Nov 30 10:48:47 2011 -0800

----------------------------------------------------------------------
 .../ext/src/com/phonegap/PhoneGapExtension.java    |  102 ++++++++-------
 1 files changed, 56 insertions(+), 46 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-blackberry-webworks/blob/571aed21/framework/ext/src/com/phonegap/PhoneGapExtension.java
----------------------------------------------------------------------
diff --git a/framework/ext/src/com/phonegap/PhoneGapExtension.java b/framework/ext/src/com/phonegap/PhoneGapExtension.java
index f97edce..86f0b07 100644
--- a/framework/ext/src/com/phonegap/PhoneGapExtension.java
+++ b/framework/ext/src/com/phonegap/PhoneGapExtension.java
@@ -26,15 +26,20 @@ import com.phonegap.notification.Notification;
 import com.phonegap.util.Log;
 import com.phonegap.util.Logger;
 
+import java.lang.ref.WeakReference;
+
 /**
  * PhoneGapExtension is a BlackBerry WebWorks JavaScript extension.  It 
  * represents a single feature that can be used to access device capabilities. 
  */
 public final class PhoneGapExtension implements WidgetExtension {
 
-    // BrowserField object used to display the application
-    //
-    protected static BrowserField browser = null;
+    // Weak reference encapsulating BrowserField object used to display the application
+    // We use the weak reference because keeping a direct reference can
+    // cause memory leak issues in WebWorks. Original solution described
+    // and suggested by Tim Neil on the BlackBerry support forums.
+    // Thanks Tim!
+    protected static WeakReference browser = null;
     
     // Browser script engine
     //
@@ -56,12 +61,12 @@ public final class PhoneGapExtension implements WidgetExtension {
     //
     private static final String FEATURE_ID ="com.phonegap";
 
-	// Called when the BlackBerry Widget references this extension for the first time.
-	// It provides a list of feature IDs exposed by this extension.
-	//
-	public String[] getFeatureList() {
-		return new String[] {FEATURE_ID};
-	}
+    // Called when the BlackBerry Widget references this extension for the first time.
+    // It provides a list of feature IDs exposed by this extension.
+    //
+    public String[] getFeatureList() {
+      return new String[] {FEATURE_ID};
+    }
 
     // Called whenever a widget loads a resource that requires a feature ID that is supplied
     // in the getFeatureList
@@ -105,7 +110,7 @@ public final class PhoneGapExtension implements WidgetExtension {
     // Called so that the extension can get a reference to the configuration or browser field
object
     //
     public void register(WidgetConfig widgetConfig, BrowserField browserField) {
-        browser = browserField;
+        browser = new WeakReference(browserField);
 
         // grab widget application name and use it to generate a unique ID
         appName = widgetConfig.getName();
@@ -115,18 +120,18 @@ public final class PhoneGapExtension implements WidgetExtension {
         Notification.registerProfile();
     }
 
-	/**
-	 * Called to clean up any features when the extension is unloaded.  This is
-	 * invoked by the WebWorks Framework when another URL is loaded.
-	 *
-	 * @see net.rim.device.api.web.WidgetExtension#unloadFeatures(org.w3c.dom.Document)
-	 */
-	public void unloadFeatures(Document doc) {
-	    // Cleanup plugin resources.
-	    if (pluginManager != null) {
-	        pluginManager.destroy();
-	    }
-	}
+    /**
+     * Called to clean up any features when the extension is unloaded.  This is
+     * invoked by the WebWorks Framework when another URL is loaded.
+     *
+     * @see net.rim.device.api.web.WidgetExtension#unloadFeatures(org.w3c.dom.Document)
+     */
+    public void unloadFeatures(Document doc) {
+        // Cleanup plugin resources.
+        if (pluginManager != null) {
+            pluginManager.destroy();
+        }
+    }
 
     public static void invokeScript(final String js) {
         // Use a new thread so that JavaScript is invoked asynchronously.
@@ -146,30 +151,35 @@ public final class PhoneGapExtension implements WidgetExtension {
             }
         }).start();
     }
-	
-	/**
-	 * Invokes the PhoneGap success callback specified by callbackId.
-	 * @param callbackId   unique callback ID
-	 * @param result       PhoneGap PluginResult containing result
-	 */
-	public static void invokeSuccessCallback(String callbackId, PluginResult result) {
-		invokeScript(result.toSuccessCallbackString(callbackId));
-	}
-
-	/**
-	 * Invokes the PhoneGap error callback specified by callbackId.
-	 * @param callbackId   unique callback ID
-	 * @param result       PhoneGap PluginResult containing result
-	 */
-	public static void invokeErrorCallback(String callbackId, PluginResult result) {
-		invokeScript(result.toErrorCallbackString(callbackId));
-	}
-	
-	/**
-	 * Provides access to the browser instance for the application.
-	 */
-	public static BrowserField getBrowserField() {
-	    return browser;
+
+    /**
+     * Invokes the PhoneGap success callback specified by callbackId.
+     * @param callbackId   unique callback ID
+     * @param result       PhoneGap PluginResult containing result
+     */
+    public static void invokeSuccessCallback(String callbackId, PluginResult result) {
+      invokeScript(result.toSuccessCallbackString(callbackId));
+    }
+
+    /**
+     * Invokes the PhoneGap error callback specified by callbackId.
+     * @param callbackId   unique callback ID
+     * @param result       PhoneGap PluginResult containing result
+     */
+    public static void invokeErrorCallback(String callbackId, PluginResult result) {
+      invokeScript(result.toErrorCallbackString(callbackId));
+    }
+
+    /**
+     * Provides access to the browser instance for the application.
+     */
+    public static BrowserField getBrowserField() {
+      Object o = browser.get();
+      if ( o instanceof BrowserField ) {
+        return (BrowserField)o;
+      } else {
+        return null;
+      }
     }
 
     /**


Mime
View raw message