macOS: fix leak in CurrentKeyboardLayout (flutter/engine#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
This commit is contained in:
Chris Bracken
2024-11-07 08:45:19 -08:00
committed by GitHub
parent 59da8913aa
commit a6917332f8

View File

@@ -9,8 +9,7 @@
#import <objc/message.h>
#include "flutter/common/constants.h"
#include "flutter/shell/platform/embedder/embedder.h"
#include "flutter/fml/platform/darwin/cf_utils.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterChannels.h"
#import "flutter/shell/platform/darwin/common/framework/Headers/FlutterCodecs.h"
#import "flutter/shell/platform/darwin/macos/framework/Headers/FlutterEngine.h"
@@ -20,6 +19,7 @@
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterRenderer.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterTextInputSemanticsObject.h"
#import "flutter/shell/platform/darwin/macos/framework/Source/FlutterView.h"
#include "flutter/shell/platform/embedder/embedder.h"
#pragma mark - Static types and data.
@@ -902,17 +902,16 @@ static void CommonInit(FlutterViewController* controller, FlutterEngine* engine)
* It's returned in NSData* to enable auto reference count.
*/
static NSData* CurrentKeyboardLayoutData() {
TISInputSourceRef source = TISCopyCurrentKeyboardInputSource();
fml::CFRef<TISInputSourceRef> source(TISCopyCurrentKeyboardInputSource());
CFTypeRef layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData);
if (layout_data == nil) {
CFRelease(source);
// TISGetInputSourceProperty returns null with Japanese keyboard layout.
// Using TISCopyCurrentKeyboardLayoutInputSource to fix NULL return.
// https://github.com/microsoft/node-native-keymap/blob/5f0699ded00179410a14c0e1b0e089fe4df8e130/src/keyboard_mac.mm#L91
source = TISCopyCurrentKeyboardLayoutInputSource();
source.Reset(TISCopyCurrentKeyboardLayoutInputSource());
layout_data = TISGetInputSourceProperty(source, kTISPropertyUnicodeKeyLayoutData);
}
return (__bridge_transfer NSData*)CFRetain(layout_data);
return (__bridge NSData*)layout_data;
}
- (void)sendKeyEvent:(const FlutterKeyEvent&)event