block的本质
block本质上也是一个OC对象,它内部也有个isa指针。
block是封装了函数调用以及函数调用环境的OC对象。
//
// Created by SK on 2020/7/7.
// Copyright © 2020 SK. All rights reserved.
//
#import <Foundation/Foundation.h>
#import "Person.h"
NSString *name = @"jack";
int main(int argc, const char * argv[]) {
@autoreleasepool {
int age = 20;
static int sex = 0;
Person *person1 = [[Person alloc] init];
Person *person2 = [[Person alloc] init];
Person *person3 = [[Person alloc] init];
__strong Person *strongPerson = person1;
__weak Person *weakPerson = person2;
__block Person *blockPerson = person3;
void (^block)(void) = ^{
NSLog(@"%d-%d-%@", age, sex, name);
[strongPerson run];
[weakPerson run];
[blockPerson run];
};
block();
}
return 0;
}
我们通过执行
xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-9.0 main.m
编译一下上述代码,来看一下block的底层结构
struct __block_impl {
void *isa;
int Flags;
int Reserved;
void *FuncPtr;
};
struct __Block_byref_blockPerson_0 {
void *__isa;
__Block_byref_blockPerson_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
Person *__strong blockPerson;
};
struct __main_block_impl_0 {
struct __block_impl impl;
struct __main_block_desc_0* Desc;
int age;
int *sex;
Person *__strong strongPerson;
Person *__weak weakPerson;
__Block_byref_blockPerson_0 *blockPerson; // by ref
}
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
__Block_byref_blockPerson_0 *blockPerson = __cself->blockPerson; // bound by ref
int age = __cself->age; // bound by copy
int *sex = __cself->sex; // bound by copy
Person *__strong strongPerson = __cself->strongPerson; // bound by copy
Person *__weak weakPerson = __cself->weakPerson; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_h6_tk2fn5rd5r92tqhsv84hkj540000gn_T_main_e976c4_mi_1, age, (*sex), name);
((void (*)(id, SEL))(void *)objc_msgSend)((id)strongPerson, sel_registerName("run"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)weakPerson, sel_registerName("run"));
((void (*)(id, SEL))(void *)objc_msgSend)((id)(blockPerson->__forwarding->blockPerson), sel_registerName("run"));
}
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {_Block_object_assign((void*)&dst->strongPerson, (void*)src->strongPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->weakPerson, (void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_assign((void*)&dst->blockPerson, (void*)src->blockPerson, 8/*BLOCK_FIELD_IS_BYREF*/);}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->strongPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);_Block_object_dispose((void*)src->blockPerson, 8/*BLOCK_FIELD_IS_BYREF*/);}
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};
int main(int argc, const char * argv[]) {
/* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
int age = 20;
static int sex = 0;
Person *person1 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
Person *person2 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
Person *person3 = ((Person *(*)(id, SEL))(void *)objc_msgSend)((id)((Person *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("Person"), sel_registerName("alloc")), sel_registerName("init"));
__attribute__((objc_ownership(strong))) Person *strongPerson = person1;
__attribute__((objc_ownership(weak))) Person *weakPerson = person2;
__attribute__((__blocks__(byref))) __Block_byref_blockPerson_0 blockPerson = {(void*)0,(__Block_byref_blockPerson_0 *)&blockPerson, 33554432, sizeof(__Block_byref_blockPerson_0), __Block_byref_id_object_copy_131, __Block_byref_id_object_dispose_131, person3};
void (*block)(void) = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, age, &sex, strongPerson, weakPerson, (__Block_byref_blockPerson_0 *)&blockPerson, 570425344));
((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
}
return 0;
}
从上述代码可以看出,block的底层结构如下:
Block底层结构.png
block的类型
block有3种类型,可以通过调用class方法或者isa指针查看具体类型,最终都是继承自NSBlock类型。
-
NSGlobalBlock ( _NSConcreteGlobalBlock )
没有访问auto变量 -
NSStackBlock ( _NSConcreteStackBlock )
访问了auto变量 -
NSMallocBlock ( _NSConcreteMallocBlock )
NSStackBlock调用了copy
block的变量捕获
为了保证block内部能够正常访问外部的变量,block有个变量捕获机制
image.png
- _NSConcreteGlobalBlock类型的block调动copy,无变化
- _NSConcreteStackBlock类型的block调用copy,从栈复制到堆
- _NSConcreteMallocBlock类型的block调用copy, 引用计数增加
在ARC环境下,编译器会根据情况自动将栈上的block复制到堆上,例如:
- block作为函数返回值时
- 将block赋值给
__strong
指针时 - block作为Cocoa API中方法名含有usingBlock的方法参数时
- block作为GCD API的方法参数时
Auto类型变量
当block内部访问了对象类型的auto变量时
- 如果block是在栈上,将不会对auto变量产生强引用
- 如果block被拷贝到堆上,会调用block内部的copy函数
copy函数内部会调用_Block_object_assign函数
_Block_object_assign函数会根据auto变量的修饰符(__strong、__weak、__unsafe_unretained)做出相应的操作,形成强引用(retain)或者弱引用 - 如果block从堆上移除,会调用block内部的dispose函数
dispose函数内部会调用_Block_object_dispose函数
_Block_object_dispose函数会自动释放引用的auto变量(release)
__block修饰符
- __block可以用于解决block内部无法修改auto变量值的问题
- __block不能修饰全局变量、静态变量(static)
- 编译器会将__block变量包装成一个对象
struct __Block_byref_blockPerson_0 {
void *__isa;
__Block_byref_blockPerson_0 *__forwarding;
int __flags;
int __size;
void (*__Block_byref_id_object_copy)(void*, void*);
void (*__Block_byref_id_object_dispose)(void*);
Person *__strong blockPerson;
};
__block的内存管理
- 当block在栈上时,并不会对__block变量产生强引用
- 当block被copy到堆时
- 会调用block内部的copy函数
- copy函数内部会调用_Block_object_assign函数
- _Block_object_assign函数会对__block变量形成强引用(retain)
- 当block从堆中移除时
- 会调用block内部的dispose函数
- dispose函数内部会调用_Block_object_dispose函数
- _Block_object_dispose函数会自动释放引用的__block变量(release)
循环引用
__weak typeof(self) weakSelf = self;
网友评论