以下结论是结合参考资料和实验得来,不对地方请大家纠正:
注意不要用NSString做实验,NSString的retainCount一直为-1;
1)局部变量(无__block修饰):
只有当block被copy时,局部变量的retainCount加1,同时self的retainCount也加1;
如果block没有被copy那么局部变量的retainCount不变;Block_release时不会把局部和self的retainCount减1的(test1方法);
输出:
2012-11-26 12:02:55.822 BlockTest[1843:11303] ======无__block修饰的局部变量
2012-11-26 12:02:55.823 BlockTest[1843:11303] Initial _lo1 retain count is 1
2012-11-26 12:02:55.824 BlockTest[1843:11303] Initial self retain count is 3
2012-11-26 12:02:55.824 BlockTest[1843:11303] _lo1 retain count is 2
2012-11-26 12:02:55.825 BlockTest[1843:11303] self retain count is 4
2012-11-26 12:02:55.825 BlockTest[1843:11303] _lo1 retain count is 1
2012-11-26 12:02:55.826 BlockTest[1843:11303] self retain count is 3
2)局部变量(有__block修饰):局部变量的引用数不会受block的影响, self的retainCount受Block_copy影响加1(test2方法)
输出:
2012-11-26 12:02:55.826 BlockTest[1843:11303] ======有__block修饰的局部变量
2012-11-26 12:02:55.826 BlockTest[1843:11303] Initial _blo1 retain count is 1
2012-11-26 12:02:55.827 BlockTest[1843:11303] Initial self retain count is 3
2012-11-26 12:02:55.827 BlockTest[1843:11303] _blo1 retain count is 1
2012-11-26 12:02:55.828 BlockTest[1843:11303] self retain count is 4
2012-11-26 12:02:55.828 BlockTest[1843:11303] _blo1 retain count is 1
2012-11-26 12:02:55.828 BlockTest[1843:11303] self retain count is 3
3)成员变量(无__block修饰):成员变量的retainCount不受影响,self的retainCount受Block_copy影响加1(test3方法)
输出:
2012-11-26 12:02:55.829 BlockTest[1843:11303] ======无__block修饰的成员变量
2012-11-26 12:02:55.829 BlockTest[1843:11303] Initial _instanceV retain count is 1
2012-11-26 12:02:55.830 BlockTest[1843:11303] Initial self retain count is 3
2012-11-26 12:02:55.830 BlockTest[1843:11303] _instanceV retain count is 1
2012-11-26 12:02:55.831 BlockTest[1843:11303] self retain count is 4
2012-11-26 12:02:55.831 BlockTest[1843:11303] _instanceV retain count is 1
2012-11-26 12:02:55.831 BlockTest[1843:11303] self retain count is 3
4)成员变量(有__block修饰):结论同3)(test4方法)
输出:
2012-11-26 12:02:55.832 BlockTest[1843:11303] ======有__block修饰的成员变量
2012-11-26 12:02:55.832 BlockTest[1843:11303] Initial _instanceV2 retain count is 1
2012-11-26 12:02:55.833 BlockTest[1843:11303] Initial self retain count is 3
2012-11-26 12:02:55.833 BlockTest[1843:11303] _instanceV2 retain count is 1
2012-11-26 12:02:55.833 BlockTest[1843:11303] self retain count is 4
2012-11-26 12:02:55.847 BlockTest[1843:11303] _instanceV2 retain count is 1
2012-11-26 12:02:55.847 BlockTest[1843:11303] self retain count is 3
5)block中不引用任何外部变量:没有Block_copy这行代码self的retainCount是不会加1的
输出:
2012-11-26 12:07:51.966 BlockTest[1881:11303] ======block中不引用任何外部变量
2012-11-26 12:07:51.967 BlockTest[1881:11303] Initial self retain count is 3
2012-11-26 12:07:51.967 BlockTest[1881:11303] self retain count is 4
2012-11-26 12:07:51.967 BlockTest[1881:11303] self retain count is 4
2012-11-26 12:07:51.968 BlockTest[1881:11303] self retain count is 3
综上:
1)无__block修饰的局部变量只有在Block_copy时retainCount才会加1;
2)有__block修饰的局部变量和成员变量的retainCount不受Block_copy的影响;
3)Block_release不会把引用的外部变量和self的retainCount减1;
4)Block_copy会把self的retainCount加1,无论block内部有没有引用局部和成员变量;
所以Block_copy慎用!Block_release时不要忘了把局部变量和self也release一次;