[ios] fix background crash when FlutterViewController is destroyed after engine destroyed context (flutter/engine#41037)

[ios] fix background crash when FlutterViewController is destroyed after engine destroyed context
This commit is contained in:
Chris Yang 2023-04-10 20:45:27 -07:00 committed by GitHub
parent b3e7898f48
commit 94fa5f4edf
2 changed files with 39 additions and 1 deletions

View File

@ -434,7 +434,7 @@ static constexpr int kNumProfilerSamplesPerSec = 5;
_undoManagerPlugin.get().viewController = nil;
if (!_allowHeadlessExecution) {
[self destroyContext];
} else {
} else if (_shell) {
flutter::PlatformViewIOS* platform_view = [self iosPlatformView];
if (platform_view) {
platform_view->SetOwnerViewController({});

View File

@ -12,6 +12,8 @@
@implementation FlutterEngineTest
extern NSNotificationName const FlutterViewControllerWillDealloc;
- (void)testIsolateId {
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:nil];
XCTAssertNil(engine.isolateId);
@ -48,4 +50,40 @@
XCTAssertNil(engine.lifecycleChannel);
}
// https://github.com/flutter/flutter/issues/123776
- (void)testReleaseViewControllerAfterDestroyContextInHeadlessMode {
FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test"
project:nil
allowHeadlessExecution:YES];
XCTAssertNil(engine.navigationChannel);
XCTAssertNil(engine.platformChannel);
XCTAssertNil(engine.lifecycleChannel);
XCTAssertTrue([engine run]);
XCTAssertNotNil(engine.navigationChannel);
XCTAssertNotNil(engine.platformChannel);
XCTAssertNotNil(engine.lifecycleChannel);
XCTestExpectation* expectation =
[[XCTestExpectation alloc] initWithDescription:@"notification called"];
@autoreleasepool {
FlutterViewController* viewController = [[FlutterViewController alloc] initWithEngine:engine
nibName:nil
bundle:nil];
[engine setViewController:viewController];
[engine destroyContext];
[[NSNotificationCenter defaultCenter] addObserverForName:FlutterViewControllerWillDealloc
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification* _Nonnull note) {
[expectation fulfill];
}];
viewController = nil;
}
[self waitForExpectations:@[ expectation ] timeout:30.0];
XCTAssertNil(engine.navigationChannel);
XCTAssertNil(engine.platformChannel);
XCTAssertNil(engine.lifecycleChannel);
}
@end