New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-14934] Self can't be assigned weakly within an NSObject's deinit. #3952
Comments
@swift-ci create |
It's a deliberate choice for ObjC weak references to fatal error when attempting to place a deallocating object into one. Swift's own weak references instead choose to safely store nil. Both are valid, but the inconsistency is annoying. This certainly could be changed, but I'm not sure if it's the right thing to do. For a case like this, you probably don't want |
Comment by Jason Persampieri (JIRA) Agreed that in this case you wouldn't want If we did want to formalize something, I wonder if we could add Optional-like access to all self.lazyVar.cleanup() that is guaranteed to create self.lazyVar?.cleanup() that would call cleanup only if It's still up the developer to notice, so it's not perfect, but I would like to think it would lead to more attention being paid. And if changing the ObjC behavior isn't an option, it's probably the best we can hope for. But that's more a proposal than a bug fix, I guess 🙂 |
I like that idea a lot. I'd take it further and ban the non-Optional version in |
Comment by Jason Persampieri (JIRA) On further thought, that might not work since the lazy var could actually be Optional. Hmm... How could we make that work? |
Comment by Jason Persampieri (JIRA) My teammates have suggested introducing the interrobang operator for this. self.lazyVar‽.cleanup() I obviously need new teammates. |
I mean, it's kind of hard to type. How about Taking this seriously for a moment, something with |
Environment
Xcode 13.0 beta 3 (13A5192i)
Additional Detail from JIRA
md5: a4dbd7e0645831e526fb5f08d85de61d
Issue Description:
Self can't be assigned weakly within an
NSObject
'sdeinit
.Test case:
Results in
Note that this does not occur if
self
is a plain Swift class.This is not uncommon in
UIViewController
code, and can result in hard-to-debug issues.If this isn't a fixable issue, then I'll post a feature request about better handling of lazy vars in deinit.
The text was updated successfully, but these errors were encountered: