Search code examples
macoscocoansopenpanelxpc

Activity monitor: com.apple.appkit.xpc.openAndSavePanelService does not terminate


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 the 2 processes
com.apple.appkit.xpc.openAndSavePanelService ...
and
QuickLookUIService (com.apple.appkit.xpc.openAndSavePanelService ...
open as soon as my methods are called, but they never terminate.
After open/save is finished, they use only minimal CPU and little memory (19 & 3 MB).

I don't see problems 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 of the XPC processes 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."