From a6917332f87c90c6a7bc1379ec822736faefd619 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Thu, 7 Nov 2024 08:45:19 -0800 Subject: [PATCH] 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 --- .../macos/framework/Source/FlutterViewController.mm | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm index aa1d157088..9f16f26d31 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm @@ -9,8 +9,7 @@ #import #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 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