From fc69b7a79f1535f1b9304ef0d763c9089e772c7b Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Thu, 20 May 2021 19:16:17 +0200 Subject: [PATCH] fix(info): fix segmentation fault on macOS --- espanso-info/src/cocoa/native.mm | 58 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/espanso-info/src/cocoa/native.mm b/espanso-info/src/cocoa/native.mm index 9e6306f..7bf3c77 100644 --- a/espanso-info/src/cocoa/native.mm +++ b/espanso-info/src/cocoa/native.mm @@ -23,54 +23,56 @@ int32_t info_get_title(char *buffer, int32_t buffer_size) { - CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID); - int32_t result = 0; + @autoreleasepool { + CFArrayRef windows = CGWindowListCopyWindowInfo(kCGWindowListExcludeDesktopElements | kCGWindowListOptionOnScreenOnly, kCGNullWindowID); + int32_t result = 0; - if (windows) { - for (NSDictionary *window in (NSArray *)windows) { - NSNumber *ownerPid = window[(id) kCGWindowOwnerPID]; + if (windows) { + for (NSDictionary *window in (NSArray *)windows) { + NSNumber *ownerPid = window[(id) kCGWindowOwnerPID]; - NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; + NSRunningApplication *currentApp = [NSRunningApplication runningApplicationWithProcessIdentifier: [ownerPid intValue]]; - if ([currentApp isActive]) { - NSString *name = window[(id) kCGWindowName]; - if (name.length > 0) { - const char * title = [name UTF8String]; - snprintf(buffer, buffer_size, "%s", title); - result = 1; + if ([currentApp isActive]) { + NSString *name = window[(id) kCGWindowName]; + if (name.length > 0) { + const char * title = [name UTF8String]; + snprintf(buffer, buffer_size, "%s", title); + result = 1; + } + break; } - break; } - } - CFRelease(windows); + CFRelease(windows); + } } - return result; + return 0; } int32_t info_get_exec(char *buffer, int32_t buffer_size) { - NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; - NSString *bundlePath = [frontApp bundleURL].path; - const char * path = [bundlePath UTF8String]; + @autoreleasepool { + NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; + NSString *bundlePath = [frontApp bundleURL].path; + const char * path = [bundlePath UTF8String]; - snprintf(buffer, buffer_size, "%s", path); - - [bundlePath release]; + snprintf(buffer, buffer_size, "%s", path); + } return 1; } int32_t info_get_class(char *buffer, int32_t buffer_size) { - NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; - NSString *bundleId = frontApp.bundleIdentifier; - const char * bundle = [bundleId UTF8String]; + @autoreleasepool { + NSRunningApplication *frontApp = [[NSWorkspace sharedWorkspace] frontmostApplication]; + NSString *bundleId = frontApp.bundleIdentifier; + const char * bundle = [bundleId UTF8String]; - snprintf(buffer, buffer_size, "%s", bundle); - - [bundleId release]; + snprintf(buffer, buffer_size, "%s", bundle); + } return 1; } \ No newline at end of file