From 0a6485e024cb2512612927417f7d91c81552f7d9 Mon Sep 17 00:00:00 2001 From: lwouis Date: Sat, 17 Jan 2026 23:42:25 +0100 Subject: [PATCH] fix: small memory leak when opening the switcher (closes #4255) --- src/api-wrappers/AXUIElement.swift | 8 +++----- src/logic/Applications.swift | 8 +++----- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/api-wrappers/AXUIElement.swift b/src/api-wrappers/AXUIElement.swift index 35cf72d0..7deac923 100644 --- a/src/api-wrappers/AXUIElement.swift +++ b/src/api-wrappers/AXUIElement.swift @@ -193,12 +193,10 @@ extension AXUIElement { func unboxAxValue(_ attributeValue: Any, _ axType: AXValueType, as _: T.Type = T.self) -> T? { let axValue = unsafeDowncast(attributeValue as CFTypeRef, to: AXValue.self) - let result = UnsafeMutablePointer.allocate(capacity: 1) - defer { result.deallocate() } - guard AXValueGetValue(axValue, axType, result) else { - return nil + return withUnsafeTemporaryAllocation(of: T.self, capacity: 1) { buffer in + guard AXValueGetValue(axValue, axType, buffer.baseAddress!) else { return nil } + return buffer[0] } - return result.pointee } /// we combine both the normal approach and brute-force to get all possible windows diff --git a/src/logic/Applications.swift b/src/logic/Applications.swift index a127f279..ae8cb941 100644 --- a/src/logic/Applications.swift +++ b/src/logic/Applications.swift @@ -36,12 +36,10 @@ class Applications { static func removeZombieWindows() { let wIds = Windows.list.compactMap { $0.cgWindowId } guard !wIds.isEmpty else { return } - let values = UnsafeMutablePointer.allocate(capacity: wIds.count) - for (i, id) in wIds.enumerated() { - values[i] = UnsafeRawPointer(bitPattern: UInt(id)) + let rawIds: CFArray = wIds.map { UnsafeRawPointer(bitPattern: UInt($0)) }.withUnsafeBufferPointer { + CFArrayCreate(nil, UnsafeMutablePointer(mutating: $0.baseAddress), $0.count, nil) } - let rawIds = CFArrayCreate(kCFAllocatorDefault, values, wIds.count, nil) - let descriptions = (CGWindowListCreateDescriptionFromArray(rawIds) as? [[CFString: Any]]) + let descriptions = CGWindowListCreateDescriptionFromArray(rawIds) as? [[CFString: Any]] let existingWids = descriptions?.compactMap { $0[kCGWindowNumber] } as? [CGWindowID] guard let existingWids else { return } let believedAlive = Set(wIds)