__weak弱引用并不持有对象,所以赋值给__weak修饰符的变量也不会改变计数器的值.
main.m
id __strong obj3 = nil;
id __weak obj1= nil;
/*
id __weak obj1 = obj;
编译器的模拟代码例如以下:
id obj1;
objc_iniitWeak(&obj1,obj);
objc_destroyWeak(&obj1);
*/
@autoreleasepool {
id obj = [[NSObject alloc] init];
obj1 = obj;
obj3 = obj;
NSLog(@"%d",_objc_autoreleasePoolPrint());//在ARC机制下查看POOL池内的内容
NSLog(@"obj retainCount = %ld",CFGetRetainCount((__bridge CFTypeRef)obj));
NSLog(@"obj1 retainCount = %ld",CFGetRetainCount((__bridge CFTypeRef)obj1));
NSLog(@"obj3 retainCount = %ld",CFGetRetainCount((__bridge CFTypeRef)obj3));
}
NSLog(@"obj3 retainCount = %ld",CFGetRetainCount((__bridge CFTypeRef)(obj3)));
NSLog(@"obj1 retainCount = %ld",CFGetRetainCount((__bridge CFTypeRef)(obj1)));
NSLog(@"%d",_objc_autoreleasePoolPrint());
/*
打印例如以下:
objc[1029]: ##############
objc[1029]: AUTORELEASE POOLS for thread 0x7fff7455a300
objc[1029]: 1 releases pending.
objc[1029]: [0x101001000] ................ PAGE (hot) (cold)
objc[1029]: [0x101001038] ################ POOL 0x101001038
objc[1029]: ##############
2015-07-24 23:02:39.686 Objective-C对象与core Foundation对象[1029:100223] 139077936
2015-07-24 23:02:39.687 Objective-C对象与core Foundation对象[1029:100223] obj retainCount = 2 -------->>>>>>>(1)
2015-07-24 23:02:39.688 Objective-C对象与core Foundation对象[1029:100223] obj1 retainCount = 3 -------->>>>>>>(2)
2015-07-24 23:02:39.688 Objective-C对象与core Foundation对象[1029:100223] obj3 retainCount = 2 -------->>>>>>>(1)
2015-07-24 23:02:39.688 Objective-C对象与core Foundation对象[1029:100223] obj3 retainCount = 1 -------->>>>>>>(3)
2015-07-24 23:02:39.688 Objective-C对象与core Foundation对象[1029:100223] obj1 retainCount = 2 -------->>>>>>>(4)
objc[1029]: ##############
objc[1029]: AUTORELEASE POOLS for thread 0x7fff7455a300
objc[1029]: 0 releases pending.
objc[1029]: [0x101001000] ................ PAGE (hot) (cold)
objc[1029]: ##############
2015-07-24 23:02:39.688 Objective-C对象与core Foundation对象[1029:100223] 139077936
*/
/*
1:由上面(1)可知__weak修饰符修饰的变量并不会持有对象
2:由(3)可知当__strong修饰的变量超出了作用域时,不在持有对象,导致rerainCout - 1;
3:细心的读者或许已经发现 obj1的retainCount和obj的不一样呢?(这也是笔者最大的疑惑)
以下介绍笔者的思考过程
(1)__weak修饰的变量尽管是对对象的弱引用,他不改变赋值对象(obj)的引用计数(retainCount),但自身的retainCount会添加. -------->>> 但是他们是指向同一个地址的,为何retainCount不一样呢?所以这条路不通
(2)尽管他们是同一个地址,但__weak不持有对象,但__weak修饰的变量想使用对象就必须使retainCount + 1,但同一时候并不改变对象的retainCount.带着这个疑问最后让我找到了答案 -->>在通过__weak指针寻找对象的时候,它有一个修饰词,当使用他的时候就会使返回的retainCount + 1(注意这里并非retainCount本身).
4:持不持有一个对象,是看它是否导致对象的retainCount + 1;而不是看他是否指向那个地址.
*/