Skip to content

Commit

Permalink
macOS: fix leak in CurrentKeyboardLayout
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
cbracken committed Nov 7, 2024
1 parent 2be456e commit 914f2ba
Showing 1 changed file with 5 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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.

Expand Down Expand Up @@ -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<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
Expand Down

0 comments on commit 914f2ba

Please sign in to comment.