---
title: "复杂数据类型变量的引用计数与block以及__block修饰符的关系"
author: "handy.wang"
date: "2012-12-14T17:05:19Z"
tags: []
categories: ["移动开发"]
slug: "block-block"
draft: false
lastmod: "2012-12-14T17:06:23Z"
id: 1748158
url: "https://handy-wang.iteye.com/blog/1748158"
---

以下结论是结合参考资料和实验得来，不对地方请大家纠正：

 

注意不要用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一次；