macoscocoamemory-leakssingletonnsopenpanel

Memory leak? com.apple.appkit.xpc.openAndSavePanelService does never terminate or deallocate


I have a SwiftUI/AppKit app (for compatibility with macos 10.15). I have successfully implemented an open and a save file method using NSOpenPanel and NSSavePanel.

I noticed in macOS' activity monitor, that these 2 processes open as soon as my methods are called, but they never terminate:
com.apple.appkit.xpc.openAndSavePanelService ...
and
QuickLookUIService (com.apple.appkit.xpc.openAndSavePanelService ...

After open/save is finished, they use only minimal CPU and little memory (19 & 3 MB).
I don't see a problem so far, but I wonder why those helper processes don't terminate or deallocate.

Here's the code in AppDelegate:

@IBAction func openDocument(_ sender: Any?) {
    let openPanel = NSOpenPanel()
    openPanel.prompt = "Import"
    openPanel.title = "Choose a .plist file"
    openPanel.titleVisibility = .visible
    openPanel.canCreateDirectories = false
    openPanel.allowedFileTypes = ["plist"] // TODO: deprecated in macOS 12
    openPanel.setFrameAutosaveName("Open Panel")
    let result = openPanel.runModal()
    if result == .OK {
        if let fileUrl = openPanel.url {
            let path = fileUrl.path
            print("selected file to open: '\(path)'")
            loadArray(from: fileUrl)
        }
    } else if result == .cancel {
        print("Open document (Import Actions) was cancelled")
    }
}

Is this behaviour normal, or a bug or leak?


Solution

  • Ok, I found this answer: Why is NSOpenPanel/NSSavePanel showing memory leak?

    It says that NSOpenPanel/NSSavePanel are singletons where "the first time you call [them], an instance of NSOpenPanel is created and not released. This is not a leak, it's an optimisation."