block语法里直接访问属性会引发循环拥有
2015-04-22 11:56
134 查看
注意不要用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一次;
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一次;
相关文章推荐
- 嵌入式c语言 之数据结构:为什么嵌入式要选择c语言? *c的可移植性好:能在不同平台的结构访问硬件 *c具有简洁紧、使用灵活的语法机制,并能直接访问硬件。 *c具有高效的运行效率
- python 直接访问类的私有属性或方法
- 如何直接访问php实例对象中的private属性详解
- 关于struts2的namespace属性,在地址栏里直接访问action的方法 ,以及用一个jsp的表单跳到上面去
- 按钮的基本使用(不能直接访问对象的 结构体属性 的成员变量\)
- 禁止空对象属性的直接访问
- CH22.P438.....直接对元素和属性访问;
- v-for循环渲染列表时,怎么直接把从对象取出的值赋值给class属性
- OC隐藏和封装-使用点语法访问属性
- python直接访问私有属性的简单方法
- ios开发之使用block引发循环引用导致内存泄露
- js--语法--for和for-in;访问对象的属性.和[]的区别
- 关于服务端访问报错后直接跳转到错误页而不显示返回信息问题即 <customErrors> 标记的“mode”属性设置为“Off”的问题的解决方案
- JavaScript:关于事件处理程序何时可以直接访问元素的属性
- c# 由属性引发的访问权限问题 英语很重要!
- 跟大家讲一讲MRC和ARC下属性Block的循环引用问题
- 子类会继承父类的私有方法和私有属性,只是不能直接访问
- Struts2学习笔记(14)-----使用ModelDriven接口直接访问对象属性
- jsrender-for循环中访问父属性
- 懒加载时为什么不能用self直接访问属性