From 48ab41ac6337691826f31cc24ee37ed819e8e495 Mon Sep 17 00:00:00 2001 From: gaaclarke <30870216+gaaclarke@users.noreply.github.com> Date: Fri, 11 Oct 2019 11:34:11 -0700 Subject: [PATCH] Removed retain cycle from notification center. (flutter/engine#13073) --- .../ios/framework/Source/FlutterEngine.mm | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 3aa559414c..2711e2e9b8 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -118,7 +118,10 @@ NSString* const FlutterDefaultDartEntrypoint = nil; NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; [center removeObserver:self name:UIApplicationDidReceiveMemoryWarningNotification object:nil]; - [_flutterViewControllerWillDeallocObserver release]; + if (_flutterViewControllerWillDeallocObserver) { + [center removeObserver:_flutterViewControllerWillDeallocObserver]; + [_flutterViewControllerWillDeallocObserver release]; + } [super dealloc]; } @@ -176,15 +179,27 @@ NSString* const FlutterDefaultDartEntrypoint = nil; self.iosPlatformView->SetOwnerViewController(_viewController); [self maybeSetupPlatformViewChannels]; + __block FlutterEngine* blockSelf = self; self.flutterViewControllerWillDeallocObserver = [[NSNotificationCenter defaultCenter] addObserverForName:FlutterViewControllerWillDealloc object:viewController queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification* note) { - [self notifyViewControllerDeallocated]; + [blockSelf notifyViewControllerDeallocated]; }]; } +- (void)setFlutterViewControllerWillDeallocObserver:(id)observer { + if (observer != _flutterViewControllerWillDeallocObserver) { + if (_flutterViewControllerWillDeallocObserver) { + [[NSNotificationCenter defaultCenter] + removeObserver:_flutterViewControllerWillDeallocObserver]; + [_flutterViewControllerWillDeallocObserver release]; + } + _flutterViewControllerWillDeallocObserver = [observer retain]; + } +} + - (void)notifyViewControllerDeallocated { if (!_allowHeadlessExecution) { [self destroyContext];