ARC weak reference zeroing timing under concurrency
0 reputation · 27 Feb 2022, 22:32 UTC
Objective-C ARC weak references are documented to zero automatically when the referenced object is deallocated. The decision point concerns safe use of those weak references across threads where deallocation and access can interleave.
The relevant constraints are ARC memory management with the weak qualifier, and the documented non-deterministic timing of weak pointer zeroing in multi-threaded environments. The distinction between weak and unowned semantics is also relevant for lifetime guarantees.
Is weak pointer zeroing guaranteed to be atomic with respect to concurrent reads on Objective-C runtimes? What synchronization guarantees, if any, does ARC provide for a weak reference read that occurs concurrently with the referent’s deallocation? When is unowned considered safe relative to weak for cross-thread object graphs under ARC?