非ARC环境block类型
-
__NSGlobalBlock__
- 没有访问auto
变量 (访问的是static ,global或者没有访问变量)
//非ARC
void(^block)(void) = ^{
NSLog(@"hello");
}; //__NSGlobalBlock__
-
__NSStackBlock__
- 访问了auto
变量
//非ARC
int a = 10;
void(^block)(void) = ^{
NSLog(@"hello:%d",a);
}; //__NSStackBlock__
-
__NSMallocBlock__
-__NSStackBlock__
调用了copy
(__NSGlobalBlock__
调用copy仍然是__NSGlobalBlock__
)
//非ARC
int a = 10;
void(^block)(void) = [^{
NSLog(@"hello:%d",a);
} copy]; //__NSMallocBlock__
ARC环境block
ARC环境下编译器会根据以下情况将栈区block复制到堆区
- block作为返回值
- 使用强指针(
__strong
)指向block - GCD函数中的block参数
- cocoa API中使用的usingBlock方法参数
property属性
-
非ARC
@property(copy , nonatomic)void (^block)(void); -
ARC
@property(strong , nonatomic)void (^block)(void);
@property(copy , nonatomic)void (^block)(void);
对象类型的auto变量
-
__NSStackBlock__
无论是访问__strong还是__weak对象类型的auto变量,都不会强引用 -
__NSMallocBlock__
访问__strong对象类型的auto变量,会强引用;
访问__weak修饰的对象类型auto变量,不会强引用 -
block访问oc对象类型的auto变量会进行内存管理
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->p, (void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->p, 3/*BLOCK_FIELD_IS_OBJECT*/);}
static struct __main_block_desc_0 {
size_t reserved;
size_t Block_size;
void (*copy)(struct __main_block_impl_0*, struct __main_block_impl_0*);
void (*dispose)(struct __main_block_impl_0*);
} __main_block_desc_0_DATA = { 0, sizeof(struct __main_block_impl_0), __main_block_copy_0, __main_block_dispose_0};
网友评论