Make plugins and CommandQueue use weak refs for CDVViewController.

Project: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/commit/88302e18
Tree: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/tree/88302e18
Diff: http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/diff/88302e18

Branch: refs/heads/master
Commit: 88302e182e97e9a4f25ccea7266d59d59a4dc7f3
Parents: 15f8a5d
Author: Andrew Grieve <agri...@chromium.org>
Authored: Fri Oct 5 14:43:46 2012 -0400
Committer: Andrew Grieve <agri...@chromium.org>
Committed: Fri Oct 5 14:43:46 2012 -0400

----------------------------------------------------------------------
 CordovaLib/Classes/CDVCommandQueue.h        |    4 ++-
 CordovaLib/Classes/CDVCommandQueue.m        |    6 +++++
 CordovaLib/Classes/CDVPlugin.h              |    8 ++++--
 CordovaLib/Classes/CDVPlugin.m              |    7 ++++++
 CordovaLib/Classes/CDVViewController.h      |    1 -
 CordovaLib/Classes/CDVViewController.m      |   24 +++++----------------
 CordovaLibTests/CordovaLibApp/AppDelegate.m |    2 -
 7 files changed, 27 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLib/Classes/CDVCommandQueue.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandQueue.h 
b/CordovaLib/Classes/CDVCommandQueue.h
index 9195259..d3e2b0a 100644
--- a/CordovaLib/Classes/CDVCommandQueue.h
+++ b/CordovaLib/Classes/CDVCommandQueue.h
@@ -25,7 +25,7 @@
 @interface CDVCommandQueue : NSObject {
     @private
     NSInteger _lastCommandQueueFlushRequestId;
-    CDVViewController* _viewController;
+    __unsafe_unretained CDVViewController* _viewController;
     NSMutableArray* _queue;
     BOOL _currentlyExecuting;
 }
@@ -33,6 +33,8 @@
 @property (nonatomic, readonly) BOOL currentlyExecuting;
 
 - (id)initWithViewController:(CDVViewController*)viewController;
+- (void)dispose;
+
 - (void)resetRequestId;
 - (void)enqueCommandBatch:(NSString*)batchJSON;
 

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLib/Classes/CDVCommandQueue.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVCommandQueue.m 
b/CordovaLib/Classes/CDVCommandQueue.m
index 8c52e39..cec5afd 100644
--- a/CordovaLib/Classes/CDVCommandQueue.m
+++ b/CordovaLib/Classes/CDVCommandQueue.m
@@ -36,6 +36,12 @@
     return self;
 }
 
+- (void)dispose
+{
+    // TODO(agrieve): Make this a zeroing weak ref once we drop support for 
4.3.
+    _viewController = nil;
+}
+
 - (void)resetRequestId
 {
     _lastCommandQueueFlushRequestId = 0;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLib/Classes/CDVPlugin.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPlugin.h b/CordovaLib/Classes/CDVPlugin.h
index 16334c8..f9b1377 100644
--- a/CordovaLib/Classes/CDVPlugin.h
+++ b/CordovaLib/Classes/CDVPlugin.h
@@ -28,10 +28,11 @@
 
 @interface CDVPlugin : NSObject {}
 
-@property (nonatomic, strong) UIWebView* webView;
+// TODO(agrieve): Make these zeroing weak refs once we drop support for 4.3.
+@property (nonatomic, unsafe_unretained) UIWebView* webView;
 @property (nonatomic, strong) NSDictionary* settings;
-@property (nonatomic, strong) UIViewController* viewController;
-@property (nonatomic, strong) id <CDVCommandDelegate> commandDelegate;
+@property (nonatomic, unsafe_unretained) UIViewController* viewController;
+@property (nonatomic, unsafe_unretained) id <CDVCommandDelegate> 
commandDelegate;
 
 @property (readonly, assign) BOOL hasPendingOperation;
 
@@ -42,6 +43,7 @@
 - (void)onAppTerminate;
 - (void)onMemoryWarning;
 - (void)onReset;
+- (void)dispose;
 
 /*
  // see initWithWebView implementation

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLib/Classes/CDVPlugin.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVPlugin.m b/CordovaLib/Classes/CDVPlugin.m
index 5039396..022d1fe 100644
--- a/CordovaLib/Classes/CDVPlugin.m
+++ b/CordovaLib/Classes/CDVPlugin.m
@@ -63,6 +63,13 @@
     return self;
 }
 
+- (void)dispose
+{
+    viewController = nil;
+    commandDelegate = nil;
+    webView = nil;
+}
+
 /*
 // NOTE: for onPause and onResume, calls into JavaScript must not call or 
trigger any blocking UI, like alerts
 - (void) onPause {}

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLib/Classes/CDVViewController.h
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.h 
b/CordovaLib/Classes/CDVViewController.h
index a4aa41c..6f723a3 100644
--- a/CordovaLib/Classes/CDVViewController.h
+++ b/CordovaLib/Classes/CDVViewController.h
@@ -47,7 +47,6 @@
 + (NSDictionary*)getBundlePlist:(NSString*)plistName;
 + (NSString*)applicationDocumentsDirectory;
 
-- (void)dispose;
 - (void)printMultitaskingInfo;
 - (void)createGapView;
 - (CDVCordovaView*)newCordovaViewWithFrame:(CGRect)bounds;

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLib/Classes/CDVViewController.m
----------------------------------------------------------------------
diff --git a/CordovaLib/Classes/CDVViewController.m 
b/CordovaLib/Classes/CDVViewController.m
index afdf75c..8573112 100644
--- a/CordovaLib/Classes/CDVViewController.m
+++ b/CordovaLib/Classes/CDVViewController.m
@@ -102,23 +102,6 @@
     return self;
 }
 
-// TODO(agrieve): It's probably better to change these to be weak references.
-- (void)dispose
-{
-    for (CDVPlugin* plugin in [self.pluginObjects allValues]) {
-        if ([plugin respondsToSelector:@selector(setViewController:)]) {
-            [plugin setViewController:nil];
-        }
-
-        if ([plugin respondsToSelector:@selector(setCommandDelegate:)]) {
-            [plugin setCommandDelegate:nil];
-        }
-    }
-
-    self.webView.delegate = nil;
-    self.webView = nil;
-}
-
 - (void)printDeprecationNotice
 {
     if (!IsAtLeastiOSVersion(@"4.2")) {
@@ -789,7 +772,7 @@ BOOL gSplashScreenShown = NO;
 - (void)evalJsHelper:(NSString*)js
 {
     void (^doIt)() = ^{
-        NSString* commandsJSON = [self.webView 
stringByEvaluatingJavaScriptFromString:js];
+        NSString* commandsJSON = [webView 
stringByEvaluatingJavaScriptFromString:js];
         [_commandQueue enqueCommandBatch:commandsJSON];
     };
 
@@ -995,6 +978,11 @@ format: &format errorDescription : &errorDesc];
     [[NSNotificationCenter defaultCenter] removeObserver:self 
name:UIApplicationWillEnterForegroundNotification object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self 
name:UIApplicationDidBecomeActiveNotification object:nil];
     [[NSNotificationCenter defaultCenter] removeObserver:self 
name:UIApplicationDidEnterBackgroundNotification object:nil];
+
+    self.webView.delegate = nil;
+    self.webView = nil;
+    [_commandQueue dispose];
+    [[self.pluginObjects allValues] 
makeObjectsPerformSelector:@selector(dispose)];
 }
 
 @end

http://git-wip-us.apache.org/repos/asf/incubator-cordova-ios/blob/88302e18/CordovaLibTests/CordovaLibApp/AppDelegate.m
----------------------------------------------------------------------
diff --git a/CordovaLibTests/CordovaLibApp/AppDelegate.m 
b/CordovaLibTests/CordovaLibApp/AppDelegate.m
index f8e93e4..4bd7906 100644
--- a/CordovaLibTests/CordovaLibApp/AppDelegate.m
+++ b/CordovaLibTests/CordovaLibApp/AppDelegate.m
@@ -52,8 +52,6 @@
 
 - (void)destroyViewController
 {
-    // Clean up circular refs so that the view controller will actually be 
released.
-    [self.viewController dispose];
     self.viewController = nil;
 }
 

Reply via email to