diff --git a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm index 3aa559414c6f5..2711e2e9b82ab 100644 --- a/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm +++ b/shell/platform/darwin/ios/framework/Source/FlutterEngine.mm @@ -118,7 +118,10 @@ - (void)dealloc { 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 @@ - (void)setViewController:(FlutterViewController*)viewController { 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];