weak指针的建立
weak修饰对象不增加其引用计数,apple通过一个hash表来实现对象的弱引用。
在Xcode下编写如下代码:
__weak obj1 = obj;
编译器编译之后变成类似如下的代码:
objc_initWeak(&obj1,obj);
翻开runtime源码,NSObject.mm,找到objc_initWeak函数,实现如下:
* @param location Address of __weak ptr.
* @param newObj Object ptr.
*/
id
objc_initWeak(id *location, id newObj)
{
if (!newObj) {
*location = nil;
return nil;
}
return storeWeak<false/*old*/, true/*new*/, true/*crash*/>(location, (objc_object*)newObj);
}
apple对参数已有说明,location是weak指针的地址,newObj是被指向的对象的地址。接下来寻找storeWeak函数,先看apple对storeWeak函数的注释:
// Update a weak variable.
// If HaveOld is true, the variable has an existing value
// that needs to be cleaned up. This value might be nil.
// If HaveNew is true, there is a new value that needs to be
// assigned into the variable. This value might be nil.
// If CrashIfDeallocating is true, the process is halted if newObj is
// deallocating or newObj’s class does not support weak references.
// If CrashIfDeallocating is false, nil is stored instead.
如果weak指针有指向的对象,那么清除这个对象,然后weak指针指向新的对象;如果weak指针指向新的对象,那么就将新的weak引用存起来;如果weak指针指向的对象被释放了,那么就不会存储这个weak引用,直接返回nil。下面一行行看代码:
template <bool HaveOld, bool HaveNew, bool CrashIfDeallocating>
static id
storeWeak(id *location, objc_object *newObj)
{
assert(HaveOld || HaveNew);
if (!HaveNew) assert(newObj == nil);
Class previouslyInitializedClass = nil;
id oldObj;
SideTable *oldTable;
SideTable *newTable;
// Acquire locks for old and new values.
// Order by lock address to prevent lock ordering problems.
// Retry if the old value changes underneath us.
retry:
if (HaveOld) {
oldObj = *location;
oldTable &