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 7546e4eabc4..0554034a663 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 @@ -673,7 +673,9 @@ static void SendFakeTouchEvent(FlutterEngine* engine, fml::WeakPtr weakSelf = [self getWeakPtr]; FlutterSendKeyEvent sendEvent = ^(const FlutterKeyEvent& event, FlutterKeyEventCallback callback, void* userData) { - [weakSelf.get()->_engine.get() sendKeyEvent:event callback:callback userData:userData]; + if (weakSelf) { + [weakSelf.get()->_engine.get() sendKeyEvent:event callback:callback userData:userData]; + } }; [self.keyboardManager addPrimaryResponder:[[[FlutterEmbedderKeyResponder alloc] initWithSendEvent:sendEvent] autorelease]]; 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 4f7aa789078..a91101c3680 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 @@ -10,6 +10,7 @@ #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterBinaryMessenger.h" #import "flutter/shell/platform/darwin/common/framework/Headers/FlutterMacros.h" #import "flutter/shell/platform/darwin/ios/framework/Headers/FlutterViewController.h" +#import "flutter/shell/platform/darwin/ios/framework/Source/FlutterEmbedderKeyResponder.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterFakeKeyEvents.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterTextInputPlugin.h" #import "flutter/shell/platform/darwin/ios/framework/Source/FlutterViewController_Internal.h" @@ -116,6 +117,15 @@ typedef enum UIAccessibilityContrast : NSInteger { @end #endif +@interface FlutterKeyboardManager (Tests) +@property(nonatomic, retain, readonly) + NSMutableArray>* primaryResponders; +@end + +@interface FlutterEmbedderKeyResponder (Tests) +@property(nonatomic, copy, readonly) FlutterSendKeyEvent sendEvent; +@end + @interface FlutterViewController (Tests) - (void)surfaceUpdated:(BOOL)appeared; - (void)performOrientationUpdate:(UIInterfaceOrientationMask)new_preferences; @@ -130,6 +140,7 @@ typedef enum UIAccessibilityContrast : NSInteger { - (void)startKeyBoardAnimation:(NSTimeInterval)duration; - (void)ensureViewportMetricsIsCorrect; - (void)invalidateDisplayLink; +- (void)addInternalPlugins; @end @interface FlutterViewControllerTest : XCTestCase @@ -392,6 +403,24 @@ typedef enum UIAccessibilityContrast : NSInteger { OCMVerify([mockEngine attachView]); } +- (void)testInternalPluginsWeakPtrNotCrash { + FlutterSendKeyEvent sendEvent; + @autoreleasepool { + FlutterViewController* vc = [[FlutterViewController alloc] initWithProject:nil + nibName:nil + bundle:nil]; + [vc addInternalPlugins]; + FlutterKeyboardManager* keyboardManager = vc.keyboardManager; + FlutterEmbedderKeyResponder* keyPrimaryResponder = (FlutterEmbedderKeyResponder*) + [(NSArray>*)keyboardManager.primaryResponders firstObject]; + sendEvent = [keyPrimaryResponder sendEvent]; + } + + if (sendEvent) { + sendEvent({}, nil, nil); + } +} + - (void)testBinaryMessenger { FlutterViewController* vc = [[FlutterViewController alloc] initWithEngine:self.mockEngine nibName:nil