Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f67f1c8

Browse files
authored
macOS: fix leak in CurrentKeyboardLayout (#56420)
Previously, if `layout_data` was not nil, we failed to `CFRelease` `source`. However, if `layout_data` was nil, we dif free source, then immediately set it to a new CoreFoundation object, which we then failed to free. We now use `fml::CFRef` which just does the right thing. Finally, we were returning `(__brdige_transfer)CFRetain(layout_data)` but `__bridge_transfer` releases the transferred object at the end of the enclosing expression, so this is equivalent to `(__bridge)layout_data`, which is what we now do. No tests since this is a refactor with no semantic changes, other than fixing an internal leak. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
1 parent d6ba01c commit f67f1c8

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

shell/platform/darwin/macos/framework/Source/FlutterViewController.mm

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@
99
#import <objc/message.h>
1010

1111
#include "flutter/common/constants.h"
12-
#include "flutter/shell/platform/embedder/embedder.h"
13-
12+
#include "flutter/fml/platform/darwin/cf_utils.h"
1413
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
1514
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
1615
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
@@ -20,6 +19,7 @@
2019
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h"
2120
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObject.h"
2221
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
22+
#include "flutter/shell/platform/embedder/embedder.h"
2323

2424
#pragma mark - Static types and data.
2525

@@ -902,17 +902,16 @@ - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey {
902902
* It's returned in NSData* to enable auto reference count.
903903
*/
904904
static NSData* CurrentKeyboardLayoutData() {
905-
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
905+
fml::CFRef<TISInputSourceRef> source(TISCopyCurrentKeyboardInputSource());
906906
CFTypeRef layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData);
907907
if (layout_data == nil) {
908-
CFRelease(source);
909908
// TISGetInputSourceProperty returns null with Japanese keyboard layout.
910909
// Using TISCopyCurrentKeyboardLayoutInputSource to fix NULL return.
911910
// https://github.com/microsoft/node-native-keymap/blob/5f0699ded00179410a14c0e1b0e089fe4df8e130/src/keyboard_mac.mm#L91
912-
source = TISCopyCurrentKeyboardLayoutInputSource();
911+
source.Reset(TISCopyCurrentKeyboardLayoutInputSource());
913912
layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData);
914913
}
915-
return (__bridge_transfer NSData*)CFRetain(layout_data);
914+
return (__bridge NSData*)layout_data;
916915
}
917916

918917
- (void)sendKeyEvent:(const FlutterKeyEvent&)event

0 commit comments

Comments
 (0)