Skip to content

Commit 6ac63dc

Browse files
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: flutter/flutter#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
1 parent cccb49d commit 6ac63dc

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngine.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,7 @@ - (void)engineCallbackOnPreEngineRestart {
11241124
while ((nextViewController = [viewControllerEnumerator nextObject])) {
11251125
[nextViewController onPreEngineRestart];
11261126
}
1127+
[_platformViewController reset];
11271128
}
11281129

11291130
- (void)onVSync:(uintptr_t)baton {

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
*/
5959
- (void)disposePlatformViews;
6060

61+
/**
62+
* Removes all platform views.
63+
*/
64+
- (void)reset;
65+
6166
@end
6267

6368
#endif // FLUTTER_SHELL_PLATFORM_DARWIN_MACOS_FRAMEWORK_SOURCE_FLUTTERPLATFORMVIEWCONTROLLER_H_

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewController.mm

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,11 @@ - (void)disposePlatformViews {
170170
_platformViewsToDispose.clear();
171171
}
172172

173+
- (void)reset {
174+
for (const auto& pair : _platformViews) {
175+
_platformViewsToDispose.insert(pair.first);
176+
}
177+
[self disposePlatformViews];
178+
}
179+
173180
@end

engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterPlatformViewControllerTest.mm

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,60 @@
141141
EXPECT_TRUE(disposed);
142142
}
143143

144+
TEST(FlutterPlatformViewController, TestReset) {
145+
// Use id so we can access handleMethodCall method.
146+
id platformViewController = [[FlutterPlatformViewController alloc] init];
147+
TestFlutterPlatformViewFactory* factory = [TestFlutterPlatformViewFactory alloc];
148+
149+
[platformViewController registerViewFactory:factory withId:@"MockPlatformView"];
150+
151+
__block bool created = false;
152+
FlutterResult resultOnCreate = ^(id result) {
153+
// If a platform view is successfully created, the result is nil.
154+
if (result == nil) {
155+
created = true;
156+
} else {
157+
created = false;
158+
}
159+
};
160+
161+
// Create 2 views.
162+
FlutterMethodCall* methodCallOnCreate0 =
163+
[FlutterMethodCall methodCallWithMethodName:@"create"
164+
arguments:@{
165+
@"id" : @0,
166+
@"viewType" : @"MockPlatformView"
167+
}];
168+
169+
[platformViewController handleMethodCall:methodCallOnCreate0 result:resultOnCreate];
170+
EXPECT_TRUE(created);
171+
172+
FlutterMethodCall* methodCallOnCreate1 =
173+
[FlutterMethodCall methodCallWithMethodName:@"create"
174+
arguments:@{
175+
@"id" : @1,
176+
@"viewType" : @"MockPlatformView"
177+
}];
178+
[platformViewController handleMethodCall:methodCallOnCreate1 result:resultOnCreate];
179+
EXPECT_TRUE(created);
180+
181+
TestFlutterPlatformView* view = nil;
182+
183+
// Before the reset, the views exist.
184+
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
185+
EXPECT_TRUE(view != nil);
186+
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
187+
EXPECT_TRUE(view != nil);
188+
189+
// After a reset, the views should no longer exist.
190+
[platformViewController reset];
191+
192+
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:0];
193+
EXPECT_TRUE(view == nil);
194+
view = (TestFlutterPlatformView*)[platformViewController platformViewWithID:1];
195+
EXPECT_TRUE(view == nil);
196+
}
197+
144198
TEST(FlutterPlatformViewController, TestDisposeOnMissingViewId) {
145199
// Use id so we can access handleMethodCall method.
146200
id platformViewController = [[FlutterPlatformViewController alloc] init];

0 commit comments

Comments
 (0)