Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit 2076a39

Browse files
committed
Clean up crash when NSApp wasn't a FlutterApplication
1 parent f06fefb commit 2076a39

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

shell/platform/darwin/macos/framework/Source/FlutterApplication.mm

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,19 @@ + (NSApplication*)sharedApplication {
2222
// +sharedApplication initializes the global NSApp, so if we're delivering
2323
// something other than a FlutterApplication, warn the developer once.
2424
#ifndef FLUTTER_RELEASE
25-
static bool notified = false;
26-
if (!notified && ![NSApp isKindOfClass:[FlutterApplication class]]) {
27-
NSLog(@"NSApp should be of type %s, not %s. "
28-
"Some application lifecycle requests (e.g. ServicesBinding.exitApplication) "
29-
"and notifications will be unavailable.\n"
30-
"Modify the application's NSPrincipleClass to be %s"
31-
"in the Info.plist to fix this.",
32-
[[self className] UTF8String], [[NSApp className] UTF8String],
33-
[[self className] UTF8String]);
34-
notified = true;
35-
}
25+
static dispatch_once_t onceToken = 0;
26+
dispatch_once(&onceToken, ^{
27+
if (![app respondsToSelector:@selector(terminateApplication:)]) {
28+
NSLog(@"NSApp should be of type %s, not %s.\n"
29+
"System requests for the application to exit will not be sent to "
30+
"the Flutter framework, so the framework will be unable to cancel "
31+
"those requests.\n"
32+
"Modify the application's NSPrincipleClass to be %s in the "
33+
"Info.plist to fix this.",
34+
[[self className] UTF8String], [[NSApp className] UTF8String],
35+
[[self className] UTF8String]);
36+
}
37+
});
3638
#endif // !FLUTTER_RELEASE
3739
return app;
3840
}
@@ -62,16 +64,17 @@ + (NSApplication*)sharedApplication {
6264
// it if it is OK to terminate. When that method channel call returns with a
6365
// result, the application either terminates or continues running.
6466
- (void)terminate:(id)sender {
65-
FlutterEngineTerminationHandler* terminationHandler =
66-
[static_cast<FlutterAppDelegate*>([NSApp delegate]) terminationHandler];
67-
if (terminationHandler) {
68-
[terminationHandler requestApplicationTermination:self
69-
exitType:kFlutterAppExitTypeCancelable
70-
result:nil];
71-
} else {
67+
FlutterAppDelegate* delegate = [self delegate];
68+
if (!delegate || ![delegate respondsToSelector:@selector(terminationHandler)] ||
69+
[delegate terminationHandler] == nil) {
7270
// If there's no termination handler, then just terminate.
7371
[super terminate:sender];
7472
}
73+
FlutterEngineTerminationHandler* terminationHandler =
74+
[static_cast<FlutterAppDelegate*>([self delegate]) terminationHandler];
75+
[terminationHandler requestApplicationTermination:sender
76+
exitType:kFlutterAppExitTypeCancelable
77+
result:nil];
7578
// Return, don't exit. The application delegate is responsible for exiting on
7679
// its own by calling |-terminateApplication|.
7780
}

shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,12 @@ - (instancetype)initWithEngine:(FlutterEngine*)engine
171171
_terminator = terminator ? terminator : ^(id sender) {
172172
// Default to actually terminating the application. The terminator exists to
173173
// allow tests to override it so that an actual exit doesn't occur.
174-
[[FlutterApplication sharedApplication] terminateApplication:sender];
174+
FlutterApplication* flutterApp = [FlutterApplication sharedApplication];
175+
if (flutterApp && [flutterApp respondsToSelector:@selector(terminateApplication:)]) {
176+
[[FlutterApplication sharedApplication] terminateApplication:sender];
177+
} else if (flutterApp) {
178+
[flutterApp terminate:sender];
179+
}
175180
};
176181
FlutterAppDelegate* appDelegate =
177182
(FlutterAppDelegate*)[[FlutterApplication sharedApplication] delegate];

0 commit comments

Comments
 (0)