My question is how the current versions of Foundation (or of the Objective-C runtime library, since this seems to be there) implement retain count for NSObject
derived objects? As I could see at NSObject.mm, there is no ivar called retain count
in the NSObject
's interface body. Instead, there seems to be a kind of table or map which contains references counters for each object. But if retain count is really done with a map, aren't retain
and release
operations too expensive with this kind of implementation (since, in this case, it's necessary to lock and unlock mutexes, lookup the map to find the right object, besides the fact that, in a multithreaded environment, only one object can be retained/released at a time)?
I didn't find anything related to setting the retain counter to 1 when allocating a new object, neither in _objc_rootAllocWithZone
at NSObject.mm (which seems to be the function that is called by [NSObject alloc]
) nor in _class_createInstanceFromZone
at objc-runtime-new.mm (that gets called later by _objc_rootAllocWithZone
).