From 914f2ba0c62ccadd3082388dd9e68eaf6a873a84 Mon Sep 17 00:00:00 2001 From: Chris Bracken Date: Wed, 6 Nov 2024 16:40:07 -0800 Subject: [PATCH] macOS: fix leak in CurrentKeyboardLayout 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. --- .../macos/framework/Source/FlutterViewController.mm | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm b/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm index aa1d15708814f..9f16f26d31f91 100644 --- a/shell/platform/darwin/macos/framework/Source/FlutterViewController.mm +++ b/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 @@ - (NSObject*)valuePublishedByPlugin:(NSString*)pluginKey { * 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