With the advent of ARC, some new functions were made available to allow developers to play around with weakly referenced objects. id objc_loadWeak(id *location)
is one of them. This function receives one parameter correspoding to a location in memory where a weak object was stored and returns this object if it is still alive or nil
if it were deallocated.
It seems that when an object obj
is stored as weak
in a location location
with id objc_storeWeak(id *location, id obj)
, obj
is put in a "weak map", with location
as a key. However, in order to retrieve obj
, objc_loadWeak
can not only use location
as a key and return the value, which corresponds to obj
. It must also check whether obj
is still alive to return nil
if it is not anymore.
However, objc_loadWeak
can not try to read the object's retain count, because the object may have been deallocated. Moreover, although the weak map, objc_storeWeak
, objc_loadWeak
and the NSObject
class are implemented in the same file (NSObject.mm), NSObject
's dealloc
method doesn't signals to the weak map that the object that is being deallocated is going away.
So, how does the Objective-C runtime figure out whether a weak
object is still alive?
It does.
calls
which in turn invokes
which in turn calls
which finally calls
This last function looks like this:
The three highlighted lines do the magic.
SideTable
is a C++ class implemented inNSObject.mm
of which therefcnts
member variable does exactly what it sounds like it does: it holds reference counts.