From 2a92d3c06adedac7b0ee6088865b88fccffcb198 Mon Sep 17 00:00:00 2001
From: Sergey Fedorov <barracuda@macos-powerpc.org>
Date: Thu, 15 Aug 2024 22:41:23 +0800
Subject: [PATCH 16/21] Fix autorelease pools

---
 src/detection/bluetooth/bluetooth_apple.m | 13 ++++++++++++-
 src/detection/cursor/cursor_apple.m       | 12 ++++++++++++
 src/detection/font/font_apple.m           | 13 ++++++++++++-
 src/detection/monitor/monitor_apple.m     | 13 ++++++++++++-
 src/detection/os/os_apple.m               | 12 ++++++++++++
 src/detection/wmtheme/wmtheme_apple.m     | 12 ++++++++++++
 src/util/apple/osascript.h                | 10 ++++++++++
 src/util/apple/osascript.m                |  2 ++
 8 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/src/detection/bluetooth/bluetooth_apple.m b/src/detection/bluetooth/bluetooth_apple.m
index abd31a0c..d5e83094 100644
--- a/src/detection/bluetooth/bluetooth_apple.m
+++ b/src/detection/bluetooth/bluetooth_apple.m
@@ -2,8 +2,19 @@
 
 #import <IOBluetooth/IOBluetooth.h>
 
+#include <AvailabilityMacros.h>
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
+
 const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */)
 {
+    POOLSTART
     NSArray<IOBluetoothDevice*>* ioDevices = IOBluetoothDevice.pairedDevices;
     if(!ioDevices)
         return "IOBluetoothDevice.pairedDevices failed";
@@ -87,6 +98,6 @@ const char* ffDetectBluetooth(FFlist* devices /* FFBluetoothResult */)
             ffStrbufTrimRight(&device->type, ',');
         }
     }
-
+    POOLEND
     return NULL;
 }
diff --git a/src/detection/cursor/cursor_apple.m b/src/detection/cursor/cursor_apple.m
index c71c440a..c0cbbd1f 100644
--- a/src/detection/cursor/cursor_apple.m
+++ b/src/detection/cursor/cursor_apple.m
@@ -2,6 +2,16 @@
 
 #import <Foundation/Foundation.h>
 
+#include <AvailabilityMacros.h>
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
+
 static void appendColor(FFstrbuf* str, NSDictionary* color)
 {
     int r = (int) (((NSNumber*) [color valueForKey:@"red"]).doubleValue * 255);
@@ -19,6 +29,7 @@ static void appendColor(FFstrbuf* str, NSDictionary* color)
 
 void ffDetectCursor(FFCursorResult* result)
 {
+    POOLSTART
     NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/com.apple.universalaccess.plist"]];
 
     NSDictionary* color;
@@ -41,4 +52,5 @@ void ffDetectCursor(FFCursorResult* result)
         ffStrbufAppendF(&result->size, "%d", (int) (mouseDriverCursorSize.doubleValue * 32));
     else
         ffStrbufAppendS(&result->size, "32");
+    POOLEND
 }
diff --git a/src/detection/font/font_apple.m b/src/detection/font/font_apple.m
index 683acad0..a2c9fb74 100644
--- a/src/detection/font/font_apple.m
+++ b/src/detection/font/font_apple.m
@@ -4,6 +4,16 @@
 
 #import <AppKit/NSFont.h>
 
+#include <AvailabilityMacros.h>
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
+
 static void generateString(FFFontResult* font)
 {
     if(font->fonts[0].length > 0)
@@ -23,6 +33,7 @@ static void generateString(FFFontResult* font)
 
 const char* ffDetectFontImpl(FFFontResult* result)
 {
+    POOLSTART
     ffStrbufAppendS(&result->fonts[0], [NSFont systemFontOfSize:12].familyName.UTF8String);
     ffStrbufAppendS(&result->fonts[1], [NSFont userFontOfSize:12].familyName.UTF8String);
     #ifdef MAC_OS_X_VERSION_10_15
@@ -32,6 +43,6 @@ const char* ffDetectFontImpl(FFFontResult* result)
     #endif
     ffStrbufAppendS(&result->fonts[3], [NSFont userFixedPitchFontOfSize:12].familyName.UTF8String);
     generateString(result);
-
+    POOLEND
     return NULL;
 }
diff --git a/src/detection/monitor/monitor_apple.m b/src/detection/monitor/monitor_apple.m
index 009577c9..c72c7f55 100644
--- a/src/detection/monitor/monitor_apple.m
+++ b/src/detection/monitor/monitor_apple.m
@@ -18,8 +18,17 @@ extern CFDictionaryRef CoreDisplay_DisplayCreateInfoDictionary(CGDirectDisplayID
 #endif
 #endif
 
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
+
 static bool detectHdrSupportWithNSScreen(FFDisplayResult* display)
 {
+    POOLSTART
     NSScreen* mainScreen = NSScreen.mainScreen;
 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 101100
     if (display->primary)
@@ -47,6 +56,7 @@ static bool detectHdrSupportWithNSScreen(FFDisplayResult* display)
         }
     }
 #endif
+    POOLEND
     return false;
 }
 
@@ -55,6 +65,7 @@ const char* ffDetectMonitor(FFlist* results)
     #ifdef MAC_OS_X_VERSION_10_15
     if(!CoreDisplay_DisplayCreateInfoDictionary) return "CoreDisplay_DisplayCreateInfoDictionary is not available";
     #endif
+    POOLSTART
     const FFDisplayServerResult* displayServer = ffConnectDisplayServer();
 
     FF_LIST_FOR_EACH(FFDisplayResult, display, displayServer->displays)
@@ -127,6 +138,6 @@ const char* ffDetectMonitor(FFlist* results)
         if (ffCfDictGetInt64(displayInfo, CFSTR("DisplayWeekManufacture"), &week) == NULL)
             monitor->manufactureWeek = (uint16_t) week;
     }
-
+    POOLEND
     return NULL;
 }
diff --git a/src/detection/os/os_apple.m b/src/detection/os/os_apple.m
index dc606c92..803bed1c 100644
--- a/src/detection/os/os_apple.m
+++ b/src/detection/os/os_apple.m
@@ -8,8 +8,19 @@
 #include <string.h>
 #include <Foundation/Foundation.h>
 
+#include <AvailabilityMacros.h>
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
+
 static void parseSystemVersion(FFOSResult* os)
 {
+    POOLSTART
     NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:
         @"/System/Library/CoreServices/SystemVersion.plist"];
 
@@ -21,6 +32,7 @@ static void parseSystemVersion(FFOSResult* os)
         ffStrbufInitS(&os->version, [value UTF8String]);
     if ((value = [dict objectForKey:@"ProductBuildVersion"]))
         ffStrbufInitS(&os->buildID, [value UTF8String]);
+    POOLEND
 }
 
 static bool detectOSCodeName(FFOSResult* os)
diff --git a/src/detection/wmtheme/wmtheme_apple.m b/src/detection/wmtheme/wmtheme_apple.m
index b78acac0..904dacbd 100644
--- a/src/detection/wmtheme/wmtheme_apple.m
+++ b/src/detection/wmtheme/wmtheme_apple.m
@@ -3,8 +3,19 @@
 
 #import <Foundation/Foundation.h>
 
+#include <AvailabilityMacros.h>
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
+
 bool ffDetectWmTheme(FFstrbuf* themeOrError)
 {
+    POOLSTART
     NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:[NSHomeDirectory() stringByAppendingPathComponent:@"/Library/Preferences/.GlobalPreferences.plist"]];
 
     NSNumber* wmThemeColor = [dict valueForKey:@"AppleAccentColor"];
@@ -28,5 +39,6 @@ bool ffDetectWmTheme(FFstrbuf* themeOrError)
 
     NSString* wmTheme = [dict valueForKey:@"AppleInterfaceStyle"];
     ffStrbufAppendF(themeOrError, " (%s)", wmTheme ? wmTheme.UTF8String : "Light");
+    POOLEND
     return true;
 }
diff --git a/src/util/apple/osascript.h b/src/util/apple/osascript.h
index 2c9fe830..f7c0b27b 100644
--- a/src/util/apple/osascript.h
+++ b/src/util/apple/osascript.h
@@ -8,3 +8,13 @@
 bool ffOsascript(const char* input, FFstrbuf* result);
 
 #endif
+
+#include <AvailabilityMacros.h>
+
+#if MAC_OS_X_VERSION_MIN_REQUIRED < 1090
+#define POOLSTART NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
+#define POOLEND   [pool release];
+#else
+#define POOLSTART
+#define POOLEND
+#endif
diff --git a/src/util/apple/osascript.m b/src/util/apple/osascript.m
index ddef3407..ebe9ae8e 100644
--- a/src/util/apple/osascript.m
+++ b/src/util/apple/osascript.m
@@ -5,6 +5,7 @@
 #include <CoreData/CoreData.h>
 
 bool ffOsascript(const char* input, FFstrbuf* result) {
+    POOLSTART
     NSString* appleScript = [NSString stringWithUTF8String: input];
     NSAppleScript* script = [[NSAppleScript alloc] initWithSource:appleScript];
     NSDictionary* errInfo = nil;
@@ -13,5 +14,6 @@ bool ffOsascript(const char* input, FFstrbuf* result) {
         return false;
 
     ffStrbufSetS(result, [[descriptor stringValue] cStringUsingEncoding:NSUTF8StringEncoding]);
+    POOLEND
     return true;
 }
-- 
2.46.0

