When any image view is being destroyed, make sure you're setting the target image to null and you've torn down any KVO "infrastructure" you've created that's tied to that object
The NSImageView is in fact in a NSTableView, and I call NSImageView.bind(_:to:withKeyPath:options:) in the callback passed to NSObject.observe(_:options:changeHandler). The observer is added when the table cell view's objectValue is set, then removed again when it is set to nil. I'll add a call to NSImageView.unbind(.value) and see if that solves the crash.
class MyCellView: NSTableCellView {
private var observer: NSKeyValueObservation?
override var objectValue: Any? {
didSet {
if let objectValue = objectValue as? MyObject {
observer = objectValue.observe(\.property) { [weak self] _, _ in
for view in subviews {
view.removeFromSuperview()
}
let imageView = NSImageView(image: nil)
imageView.bind(.value, to: objectValue, withKeyPath: keyPath)
addSubview(imageView)
}
} else {
observer = nil
}
}
}
}
That's generally a reasonable statement for the "core" of Objective-C’s memory management model
Not sure if it's relevant, but I'm using Swift, although I suspect KVO is at the Objective C level.
I would also try Zombies
Sorry, forgot to add that I also enabled that one, but no crash for now. I'll keep that enabled too.
Thanks for your precious input and I'll update you when I find out more.
Topic:
UI Frameworks
SubTopic:
AppKit
Tags: