I was noticing that my NSWindowControllers were never being deallocated when windows were closed, and I narrowed it down to an issue where an NSWindow seems to have a strong reference to its delegate, despite the
class SimpleWindow : NSWindow, NSWindowDelegate {
/// Debug variable to see how many instances are around
public private(set) static var InstanceCount = 0
deinit {
SimpleWindow.InstanceCount -= 1
}
override init(contentRect: NSRect, styleMask style: NSWindow.StyleMask, backing backingStoreType: NSWindow.BackingStoreType, defer flag: Bool) {
super.init(contentRect: contentRect, styleMask: style, backing: backingStoreType, defer: flag)
SimpleWindow.InstanceCount += 1
}
}
class WindowReferenceTests: XCTestCase {
func testWindows() {
let startCount = SimpleWindow.InstanceCount
do {
let wc = SimpleWindow()
wc.delegate = wc // weak open var delegate: NSWindowDelegate?
XCTAssertEqual(startCount + 1, SimpleWindow.InstanceCount)
}
XCTAssertEqual(startCount, SimpleWindow.InstanceCount, "window controllers have circular references somewhere")
}
}