Follow

I’ve seen some confusion about this recently, so figure it’s worth discussing: ‘unowned’ in Swift is *not* like __unsafe_unretained in ObjC, and actually is pretty odd.

The difference is just that unowned deterministically guarantees a crash if misused, but that requires a lot:
• A second reference count for unowned references is kept
• The object’s deinit runs when all strong references are gone, but it isn’t actually freed until unowned ones are too!

(Yes, unowned references still pay for atomic math; unowned(unsafe) avoids that)

This fixes the most common bizarre symptom of over-releases in ObjC: my object deallocated and a new unrelated object reused its memory. Usually this showed up as “unrecognized selector xyz sent to object of type <completely the wrong type how did this get here>”.

Sign in to participate in the conversation
Mastodon

Invite-only Mastodon server run by the main developers of the project 🐘 It is not focused on any particular niche interest - everyone is welcome as long as you follow our code of conduct!