Fix long-running desktop resource leaks

This commit is contained in:
Simos Mikelatos
2026-06-19 13:07:08 +00:00
parent 901c6fc956
commit 53c3c4c27a
3 changed files with 55 additions and 10 deletions

View File

@@ -15,6 +15,8 @@ struct ElementRecord {
var stateElements: [String: [ElementRecord]] = [:]
var stateAxElements: [String: [String: AXUIElement]] = [:]
var stateOrder: [String] = []
let maxStoredStates = 100
func jsonLine(_ object: Any) {
guard JSONSerialization.isValidJSONObject(object),
@@ -116,6 +118,14 @@ func dictionary(_ record: ElementRecord) -> JSON {
return output
}
func pruneStoredStates() {
while stateOrder.count > maxStoredStates {
let evicted = stateOrder.removeFirst()
stateElements.removeValue(forKey: evicted)
stateAxElements.removeValue(forKey: evicted)
}
}
func resolveApp(_ query: String) throws -> NSRunningApplication {
let normalized = query.lowercased()
let apps = NSWorkspace.shared.runningApplications.filter { app in
@@ -189,6 +199,8 @@ func getAppState(_ params: JSON) throws -> JSON {
let stateId = "state_\(UUID().uuidString)"
stateElements[stateId] = records
stateAxElements[stateId] = axRecords
stateOrder.append(stateId)
pruneStoredStates()
let elements = records.map(dictionary)
return [