[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: <FlutterKeyboardManager 0x29ec55f40> [80]
       8 (432 bytes) __strong _primaryResponders --> ROOT CYCLE: <NSMutableArray 0x29ec560a0> [64]
          7 (368 bytes) ROOT CYCLE: <NSMutableArray (Storage) 0x29ec56140> [32]
             4 (224 bytes) ROOT CYCLE: <FlutterEmbedderKeyResponder 0x29ec56310> [80]
                1 (48 bytes) __strong _sendEvent --> ROOT CYCLE: <__NSMallocBlock__ 0x29ec56360> [48]
                   __strong [capture] --> CYCLE BACK TO <FlutterKeyboardManager 0x29ec55f40> [80]
                1 (48 bytes) __strong _pendingResponses --> <NSMutableDictionary 0x29ec563c0> [48]
                1 (48 bytes) __strong _pressingRecords --> <NSMutableDictionary 0x29ec56390> [48]
             2 (112 bytes) <FlutterChannelKeyResponder 0x29ec56450> [48]
                1 (64 bytes) __strong _channel --> <FlutterBasicMessageChannel 0x29ec564e0> [64]
       2 (1.55K) __strong _layoutMap --> <NSMutableDictionary 0x29ec56630> [48]
          1 (1.50K) <NSMutableDictionary (Storage) 0x123c34a00> [1536]
       1 (64 bytes) __strong _pendingEvents --> <NSMutableArray 0x29ec565f0> [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.
This commit is contained in:
Sergiy Dubovik
2023-12-20 19:04:10 +02:00
committed by GitHub
parent a3cd526c3f
commit b7264de08d
2 changed files with 43 additions and 4 deletions

View File

@@ -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<NSNumber*, NSNumber*> dictionary];
[self buildLayout];
@@ -150,7 +157,6 @@ typedef _Nullable _NSResponderPtr (^NextResponderProvider)();
responder.layoutMap = _layoutMap;
}
__weak __typeof__(self) weakSelf = self;
[_viewDelegate subscribeToKeyboardLayoutChange:^() {
[weakSelf buildLayout];
}];

View File

@@ -426,9 +426,11 @@ void clearEvents(std::vector<FlutterKeyEvent>& 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