From 42f499b4f0d468ce3e3aaceab268647469d4b2b0 Mon Sep 17 00:00:00 2001 From: Wu Zhong Date: Fri, 29 Jul 2022 10:11:04 +0800 Subject: [PATCH] [iOS] Fixes SplashScreenView crash when remove from view hierarchy (flutter/engine#34496) --- .../darwin/ios/framework/Headers/FlutterViewController.h | 2 +- .../darwin/ios/framework/Source/FlutterViewController.mm | 3 ++- .../ios/framework/Source/FlutterViewControllerTest.mm | 9 +++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h index 6c67715b66..d33d989370 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h @@ -197,7 +197,7 @@ FLUTTER_DARWIN_EXPORT /** * Specifies the view to use as a splash screen. Flutter's rendering is asynchronous, so the first - * frame rendered by the Flutter application might not immediately appear when theFlutter view is + * frame rendered by the Flutter application might not immediately appear when the Flutter view is * initially placed in the view hierarchy. The splash screen view will be used as * a replacement until the first frame is rendered. * diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm index 2d9f8a4ff1..315611072f 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController.mm @@ -507,7 +507,7 @@ static void SendFakeTouchEvent(FlutterEngine* engine, - (void)removeSplashScreenView:(dispatch_block_t _Nullable)onComplete { NSAssert(_splashScreenView, @"The splash screen view must not be null"); - UIView* splashScreen = _splashScreenView.get(); + UIView* splashScreen = [_splashScreenView.get() retain]; _splashScreenView.reset(); [UIView animateWithDuration:0.2 animations:^{ @@ -515,6 +515,7 @@ static void SendFakeTouchEvent(FlutterEngine* engine, } completion:^(BOOL finished) { [splashScreen removeFromSuperview]; + [splashScreen release]; if (onComplete) { onComplete(); } diff --git a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm index 69bfdc348c..556d87f584 100644 --- a/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/ios/framework/Source/FlutterViewControllerTest.mm @@ -433,6 +433,15 @@ extern NSNotificationName const FlutterViewControllerWillDealloc; OCMVerify(never(), [mockEngine attachView]); } +- (void)testSplashScreenViewRemoveNotCrash { + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"engine" project:nil]; + [engine runWithEntrypoint:nil]; + FlutterViewController* flutterViewController = + [[FlutterViewController alloc] initWithEngine:engine nibName:nil bundle:nil]; + [flutterViewController setSplashScreenView:[[UIView alloc] init]]; + [flutterViewController setSplashScreenView:nil]; +} + - (void)testInternalPluginsWeakPtrNotCrash { FlutterSendKeyEvent sendEvent; @autoreleasepool {