From 6ac63dc0fc4ba0016c238a6b5fd97044da20b6c9 Mon Sep 17 00:00:00 2001 From: Paul Sturm Date: Wed, 19 Feb 2025 14:02:57 -0600 Subject: [PATCH] Hot Restart should dispose all previous Platform Views (macOS) (#163439) 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]. [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 --- .../macos/framework/Source/FlutterEngine.mm | 1 + .../Source/FlutterPlatformViewController.h | 5 ++ .../Source/FlutterPlatformViewController.mm | 7 +++ .../FlutterPlatformViewControllerTest.mm | 54 +++++++++++++++++++ 4 files changed, 67 insertions(+) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm index 1a87c66386..2c0f4c9bec 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm @@ -1124,6 +1124,7 @@ static void SetThreadPriority(FlutterThreadPriority priority) { while ((nextViewController = [viewControllerEnumerator nextObject])) { [nextViewController onPreEngineRestart]; } + [_platformViewController reset]; } - (void)onVSync:(uintptr_t)baton { diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h index 4123d8208f..a784a69b94 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h @@ -58,6 +58,11 @@ */ - (void)disposePlatformViews; +/** + * Removes all platform views. + */ +- (void)reset; + @end #endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWCONTROLLER_H_ diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm index 64b83cd80d..5e7230c7d1 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm @@ -170,4 +170,11 @@ _platformViewsToDispose.clear(); } +- (void)reset { + for (const auto& pair : _platformViews) { + _platformViewsToDispose.insert(pair.first); + } + [self disposePlatformViews]; +} + @end diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm index 65f59ddaf2..066eeff4dc 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm @@ -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];