From b7264de08dad0fcd12b8951641e615bcb605dd7a Mon Sep 17 00:00:00 2001 From: Sergiy Dubovik Date: Wed, 20 Dec 2023 19:04:10 +0200 Subject: [PATCH] [macos] FlutterKeyboardManager memory leak fix (flutter/engine#48824) We are embedding Flutter into MacOS app, and noticed that there is a leak. `leaks` tool says there is a cycle reference: ``` 12 (2.11K) ROOT CYCLE: [80] 8 (432 bytes) __strong _primaryResponders --> ROOT CYCLE: [64] 7 (368 bytes) ROOT CYCLE: [32] 4 (224 bytes) ROOT CYCLE: [80] 1 (48 bytes) __strong _sendEvent --> ROOT CYCLE: <__NSMallocBlock__ 0x29ec56360> [48] __strong [capture] --> CYCLE BACK TO [80] 1 (48 bytes) __strong _pendingResponses --> [48] 1 (48 bytes) __strong _pressingRecords --> [48] 2 (112 bytes) [48] 1 (64 bytes) __strong _channel --> [64] 2 (1.55K) __strong _layoutMap --> [48] 1 (1.50K) [1536] 1 (64 bytes) __strong _pendingEvents --> [64] ``` This patch uses `weak` pointer to `self` instead of implicit `strong`. ## 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] and the [C++, Objective-C, Java style guides]. - [x] I listed at least one issue that this PR fixes in the description above. - [x] I added new tests to check the change I am making or feature I am adding, or the PR is [test-exempt]. See [testing the engine] for instructions on writing and running engine tests. - [x] I updated/added relevant documentation (doc comments with `///`). - [x] I signed the [CLA]. - [x] All existing and new tests are passing. --- .../Source/FlutterKeyboardManager.mm | 14 +++++--- .../Source/FlutterKeyboardManagerTest.mm | 33 +++++++++++++++++++ 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm index 00ba4ac38c..fc0e5db075 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManager.mm @@ -123,18 +123,24 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)(); [FlutterMethodChannel methodChannelWithName:@"flutter/keyboard" binaryMessenger:[_viewDelegate getBinaryMessenger] codec:[FlutterStandardMethodCodec sharedInstance]]; + [keyboardChannel setMethodCallHandler:^(FlutterMethodCall* call, FlutterResult result) { [self handleKeyboardMethodCall:call result:result]; }]; + _primaryResponders = [[NSMutableArray alloc] init]; + + __weak __typeof__(self) weakSelf = self; [self addPrimaryResponder:[[FlutterEmbedderKeyResponder alloc] initWithSendEvent:^(const FlutterKeyEvent& event, FlutterKeyEventCallback callback, void* userData) { - [_viewDelegate sendKeyEvent:event - callback:callback - userData:userData]; + __strong __typeof__(weakSelf) strongSelf = weakSelf; + [strongSelf.viewDelegate sendKeyEvent:event + callback:callback + userData:userData]; }]]; + [self addPrimaryResponder:[[FlutterChannelKeyResponder alloc] initWithChannel:[FlutterBasicMessageChannel @@ -143,6 +149,7 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)(); getBinaryMessenger] codec:[FlutterJSONMessageCodec sharedInstance]]]]; + _pendingEvents = [[NSMutableArray alloc] init]; _layoutMap = [NSMutableDictionary dictionary]; [self buildLayout]; @@ -150,7 +157,6 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)(); responder.layoutMap = _layoutMap; } - __weak __typeof__(self) weakSelf = self; [_viewDelegate subscribeToKeyboardLayoutChange:^() { [weakSelf buildLayout]; }]; diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm index 5e57509f70..53e3144803 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterKeyboardManagerTest.mm @@ -426,9 +426,11 @@ void clearEvents(std::vector& events) { - (bool)keyboardChannelGetPressedState; - (bool)racingConditionBetweenKeyAndText; - (bool)correctLogicalKeyForLayouts; +- (bool)shouldNotHoldStrongReferenceToViewDelegate; @end namespace flutter::testing { + TEST(FlutterKeyboardManagerUnittests, SinglePrimaryResponder) { ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] singlePrimaryResponder]); } @@ -461,6 +463,11 @@ TEST(FlutterKeyboardManagerUnittests, CorrectLogicalKeyForLayouts) { ASSERT_TRUE([[FlutterKeyboardManagerUnittestsObjC alloc] correctLogicalKeyForLayouts]); } +TEST(FlutterKeyboardManagerUnittests, ShouldNotHoldStrongReferenceToViewDelegate) { + ASSERT_TRUE( + [[FlutterKeyboardManagerUnittestsObjC alloc] shouldNotHoldStrongReferenceToViewDelegate]); +} + } // namespace flutter::testing @implementation FlutterKeyboardManagerUnittestsObjC @@ -809,4 +816,30 @@ TEST(FlutterKeyboardManagerUnittests, CorrectLogicalKeyForLayouts) { return TRUE; } +- (bool)shouldNotHoldStrongReferenceToViewDelegate { + __strong FlutterKeyboardManager* strongKeyboardManager; + __weak id weakViewDelegate; + + @autoreleasepool { + id binaryMessengerMock = OCMStrictProtocolMock(@protocol(FlutterBinaryMessenger)); + OCMStub([binaryMessengerMock setMessageHandlerOnChannel:[OCMArg any] + binaryMessageHandler:[OCMArg any]]); + + id viewDelegateMock = OCMStrictProtocolMock(@protocol(FlutterKeyboardViewDelegate)); + OCMStub([viewDelegateMock getBinaryMessenger]).andReturn(binaryMessengerMock); + OCMStub([viewDelegateMock subscribeToKeyboardLayoutChange:[OCMArg any]]); + + LayoutClue layoutClue; + OCMStub([viewDelegateMock lookUpLayoutForKeyCode:0 shift:NO]) + .ignoringNonObjectArgs() + .andReturn(layoutClue); + FlutterKeyboardManager* keyboardManager = + [[FlutterKeyboardManager alloc] initWithViewDelegate:viewDelegateMock]; + strongKeyboardManager = keyboardManager; + weakViewDelegate = viewDelegateMock; + } + + return weakViewDelegate == nil; +} + @end