Hot Restart should dispose all previous Platform Views (macOS) (#163439)

<!--
Thanks for filing a pull request!
Reviewers are typically assigned within a week of filing a request.
To learn more about code review, see our documentation on Tree Hygiene:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
-->

When using Platform Views on macOS, performing a Hot Restart throws an
exception with message "trying to create an already created view". This
is because the old Platform Views are not cleaned up. So, here we
dispose of the old Platform Views as part of the Hot Restart process.

Fixes issue: https://github.com/flutter/flutter/issues/110381

## Pre-launch Checklist

- [x] I read the [Contributor Guide] and followed the process outlined
there for submitting PRs.
- [x] I read the [Tree Hygiene] wiki page, which explains my
responsibilities.
- [x] I read and followed the [Flutter Style Guide], including [Features
we expect every widget to implement].
- [x] I signed the [CLA].
- [x] I listed at least one issue that this PR fixes in the description
above.
- [x] I updated/added relevant documentation (doc comments with `///`).
- [x] I added new tests to check the change I am making, or this PR is
[test-exempt].
- [x] I followed the [breaking change policy] and added [Data Driven
Fixes] where supported.
- [x] All existing and new tests are passing.

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
This commit is contained in:
Paul Sturm 2025-02-19 14:02:57 -06:00 committed by GitHub
parent cccb49d3e6
commit 6ac63dc0fc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 67 additions and 0 deletions

View File

@ -1124,6 +1124,7 @@ static void SetThreadPriority(FlutterThreadPriority priority) {
while ((nextViewController = [viewControllerEnumerator nextObject])) {
[nextViewController onPreEngineRestart];
}
[_platformViewController reset];
}
- (void)onVSync:(uintptr_t)baton {

View File

@ -58,6 +58,11 @@
*/
- (void)disposePlatformViews;
/**
* Removes all platform views.
*/
- (void)reset;
@end
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWCONTROLLER_H_

View File

@ -170,4 +170,11 @@
_platformViewsToDispose.clear();
}
- (void)reset {
for (const auto& pair : _platformViews) {
_platformViewsToDispose.insert(pair.first);
}
[self disposePlatformViews];
}
@end

View File

@ -141,6 +141,60 @@ TEST(FlutterPlatformViewController, TestCreateAndDispose) {
EXPECT_TRUE(disposed);
}
TEST(FlutterPlatformViewController, TestReset) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];
TestFlutterPlatformViewFactory* factory = [TestFlutterPlatformViewFactory alloc];
[platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
__block bool created = false;
FlutterResult resultOnCreate = ^(id result) {
// If a platform view is successfully created, the result is nil.
if (result == nil) {
created = true;
} else {
created = false;
}
};
// Create 2 views.
FlutterMethodCall* methodCallOnCreate0 =
[FlutterMethodCall methodCallWithMethodName:@"create"
arguments:@{
@"id" : @0,
@"viewType" : @"MockPlatformView"
}];
[platformViewController handleMethodCall:methodCallOnCreate0 result:resultOnCreate];
EXPECT_TRUE(created);
FlutterMethodCall* methodCallOnCreate1 =
[FlutterMethodCall methodCallWithMethodName:@"create"
arguments:@{
@"id" : @1,
@"viewType" : @"MockPlatformView"
}];
[platformViewController handleMethodCall:methodCallOnCreate1 result:resultOnCreate];
EXPECT_TRUE(created);
TestFlutterPlatformView* view = nil;
// Before the reset, the views exist.
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
EXPECT_TRUE(view != nil);
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
EXPECT_TRUE(view != nil);
// After a reset, the views should no longer exist.
[platformViewController reset];
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
EXPECT_TRUE(view == nil);
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
EXPECT_TRUE(view == nil);
}
TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) {
// Use id so we can access handleMethodCall method.
id platformViewController = [[FlutterPlatformViewController alloc] init];