From 551fe982eb0c69c9695faff7b2f8cf2a5f10e0d7 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Tue, 8 Aug 2023 15:18:46 -0700 Subject: [PATCH] [macOS] Improve engine retain cycle testing (flutter/engine#44509) Adds testing that verifies that the engine is not retained via the FlutterTextureRegistry that is returned from the FlutterTextureRegistrar returned by the engine. This also simplifies the existing test for a retain cycle via the FlutterBinaryMessenger by avoiding manually casting the binary messenger to a FlutterBinaryMessengerRelay, or knowing any of its implementation details. Issue: https://github.com/flutter/flutter/issues/116445 [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/FlutterEngineTest.mm | 50 +++++++++++++------ 1 file changed, 35 insertions(+), 15 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm index 9b3468a33c..cf384cfd1b 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEngineTest.mm @@ -501,11 +501,12 @@ TEST_F(FlutterEngineTest, DartEntrypointArguments) { // Verify that the engine is not retained indirectly via the binary messenger held by channels and // plugins. Previously, FlutterEngine.binaryMessenger returned the engine itself, and thus plugins // could cause a retain cycle, preventing the engine from being deallocated. -// FlutterEngine.binaryMessenger now returns a FlutterBinaryMessengerRelay whose pointer back to -// the engine is cleared when the engine is deallocated. +// FlutterEngine.binaryMessenger now returns a FlutterBinaryMessengerRelay whose weak pointer back +// to the engine is cleared when the engine is deallocated. // Issue: https://github.com/flutter/flutter/issues/116445 -TEST_F(FlutterEngineTest, FlutterBinaryMessengerNullsParentOnEngineRelease) { - FlutterBinaryMessengerRelay* relay = nil; +TEST_F(FlutterEngineTest, FlutterBinaryMessengerDoesNotRetainEngine) { + __weak FlutterEngine* weakEngine; + id binaryMessenger = nil; @autoreleasepool { // Create a test engine. NSString* fixtures = @(flutter::testing::GetFixturesPath()); @@ -515,19 +516,38 @@ TEST_F(FlutterEngineTest, FlutterBinaryMessengerNullsParentOnEngineRelease) { FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" project:project allowHeadlessExecution:true]; - - // Get the binary messenger for the engine. - id binaryMessenger = engine.binaryMessenger; - ASSERT_TRUE([binaryMessenger isKindOfClass:[FlutterBinaryMessengerRelay class]]); - relay = (FlutterBinaryMessengerRelay*)binaryMessenger; - - // Verify the relay parent (the engine) is non-nil. - EXPECT_NE(relay.parent, nil); + weakEngine = engine; + binaryMessenger = engine.binaryMessenger; } - // Once the engine has been deallocated, verify the relay parent is nil, and thus the engine is - // not retained by the holder of the relay. - EXPECT_EQ(relay.parent, nil); + // Once the engine has been deallocated, verify the weak engine pointer is nil, and thus not + // retained by the relay. + EXPECT_NE(binaryMessenger, nil); + EXPECT_EQ(weakEngine, nil); +} + +// Verify that the engine is not retained indirectly via the texture registry held by plugins. +// Issue: https://github.com/flutter/flutter/issues/116445 +TEST_F(FlutterEngineTest, FlutterTextureRegistryDoesNotReturnEngine) { + __weak FlutterEngine* weakEngine; + id textureRegistry; + @autoreleasepool { + // Create a test engine. + NSString* fixtures = @(flutter::testing::GetFixturesPath()); + FlutterDartProject* project = [[FlutterDartProject alloc] + initWithAssetsPath:fixtures + ICUDataPath:[fixtures stringByAppendingString:@"/icudtl.dat"]]; + FlutterEngine* engine = [[FlutterEngine alloc] initWithName:@"test" + project:project + allowHeadlessExecution:true]; + id registrar = [engine registrarForPlugin:@"MyPlugin"]; + textureRegistry = registrar.textures; + } + + // Once the engine has been deallocated, verify the weak engine pointer is nil, and thus not + // retained via the texture registry. + EXPECT_NE(textureRegistry, nil); + EXPECT_EQ(weakEngine, nil); } // If a channel overrides a previous channel with the same name, cleaning