Found an interesting foot-gun (only traps with NSObject classes and only with Foundation on Apple platforms):
import Foundation
class C: NSObject {
lazy var foo: Int = {
// Thread 1: abort with payload or reason
// Cannot form weak reference to instance (0x1007e24f0) of class C.
// It is possible that this object was over-released, or is in the process of deallocation.
weak var y: C? = self
return 1
}()
deinit {
_ = foo
}
}
do { _ = C() }
print("ended")
The question is whether this should not trap. Or trap in all cases (including non NSObject and non Apple platforms).
The difference comes from which runtime handles the weak reference. For an NSObject subclass, weak var y = self goes through the Objective-C runtime's weak table, which deliberately aborts if you form a weak reference to an object that's already deallocating. That's the "Cannot form weak reference … in the process of deallocation" message. Pure Swift classes use Swift's native reference counting, which doesn't trap here: the weak reference just ends up nil. That explains why it only happens with NSObject on Apple platforms.
Accessing the lazy property in deinit is what triggers it, because that's the first time its initializer runs, and it captures self weakly while self is being destroyed. Practical rules: don't initialize lazy properties in deinit, and don't form new references (weak or strong) to self there. If cleanup needs the value, make it non-lazy or check whether it was ever created.
Whether it should behave the same everywhere is a fair question. Today it's a long-standing difference between the two runtimes, not something Swift itself guarantees.
Fair point, your example shows it well. Two thoughts:
1. The non-NSObject version doesn't really "work" either. Without NSObject, it doesn't crash, but deinit still creates a brand-new Foo whose delegate is already nil by the time anyone reads it. You get no crash, but you're doing pointless work on an object that's being torn down. In that sense the Objective-C runtime's trap is the more honest result. The Swift version hides the same bug.
2. For indirect access, the fix is structural, not discipline. Don't rely on remembering which methods touch foo. Make it impossible for deinit to create it. Replace lazy with explicit optional storage, so every path can tell "never created" apart from "use it":
class Bar: NSObject, FooDelegate {
private var _foo: Foo?
var foo: Foo {
if let foo = _foo { return foo }
let foo = Foo(delegate: self)
_foo = foo
return foo
}
deinit {
_foo?.someCall() // only if it already exists; never creates it
}
}
If you'd rather keep lazy, you can at least turn the obscure trap into a clear one in debug builds:
class Bar: NSObject, FooDelegate {
private var isDeinitializing = false
lazy var foo: Foo = {
assert(!isDeinitializing, "Bar.foo first accessed during deinit")
return Foo(delegate: self)
}()
deinit {
isDeinitializing = true
someCall()
}
func someCall() { foo.someCall() }
}
As for whether the language should help, a compiler warning for "deinit (transitively) triggers a lazy initializer that captures self" would be nice. But it's hard to do in general once the access goes through overridable methods or protocol requirements, so I'd expect it to remain a runtime concern.
Not necessarily pointless – there might be an observable side effect during lazy variable initialisation.
I tend to think that native Swift class behaviour (not trapping) is both easier to implement and easier to reason about (and definitely easier to live with).