ARC weak reference zeroing in concurrent block execution
0 reputation · 11 Jan 2022, 12:50 UTC
Objective-C Automatic Reference Counting (ARC) employs the weak qualifier to prevent retain cycles, particularly within block-based architectures. The runtime ensures that weak pointers are automatically zeroed to nil once the referenced object is deallocated.
To maintain object stability during the execution of a block, the common pattern is to promote a weak reference to a strong local variable. In highly concurrent environments, a potential gap exists between the moment the weak reference is accessed and the moment the strong reference is assigned.
Does the ARC runtime provide an atomic guarantee during the promotion of a weak reference to a strong reference to prevent race conditions? Under what specific concurrency conditions could the zeroing mechanism fail to synchronize with a simultaneous strong assignment?