I've been spending days trying to solve the memory leak in a small menu bar application I've wrote (SC Menu). I've used Instruments which shows the leaks and memory graph which shows unreleased allocations. This occurs when someone views a certificate on the smartcard.
Basically it opens a new window and displays the certificate, the same way Keychain Access displays a certificate. Whenever I create an SFCertificateView instance and set setDetailsDisclosed(true) - a memory leak happens. Instruments highlights that line.
import Cocoa
import SecurityInterface
class ViewCertsViewController: NSViewController {
var selectedCert: SecIdentity? = nil
override func viewDidLoad() {
super.viewDidLoad()
self.view = NSView(frame: NSRect(x: 0, y: 0, width: 500, height: 500))
self.view.wantsLayer = true
var secRef: SecCertificate? = nil
guard let selectedCert else { return }
let certRefErr = SecIdentityCopyCertificate(selectedCert, &secRef)
if certRefErr != errSecSuccess {
os_log("Error getting certificate from identity: %{public}@", log: OSLog.default, type: .error, String(describing: certRefErr))
return
}
let scrollView = NSScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.borderType = .lineBorder
scrollView.hasHorizontalScroller = true
scrollView.hasVerticalScroller = true
let certView = SFCertificateView()
guard let secRef = secRef else { return }
certView.setCertificate(secRef)
certView.setDetailsDisclosed(true)
certView.setDisplayTrust(true)
certView.setEditableTrust(true)
certView.setDisplayDetails(true)
certView.setPolicies(SecPolicyCreateBasicX509())
certView.translatesAutoresizingMaskIntoConstraints = false
scrollView.documentView = certView
view.addSubview(scrollView)
// Layout constraints
NSLayoutConstraint.activate([
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
scrollView.topAnchor.constraint(equalTo: view.topAnchor),
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
// Provide certificate view a width and height constraint
certView.widthAnchor.constraint(equalTo: scrollView.widthAnchor),
certView.heightAnchor.constraint(greaterThanOrEqualToConstant: 500)
])
}
}
https://github.com/boberito/sc_menu/blob/dev_2.0/smartcard_menu/ViewCertsViewController.swift
Fairly simple.