Objective-C Block本质

Block本质

  • block本质是一个OC对象,它内部也有个isa指针
  • block是封装了函数调用以及函数调用环境的OC对象
  • 将以下代码编译为C++源码 xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m
void (^block)(void);
void test()
{
    int age = 10;
    static int height = 10;
    block = ^{
        NSLog(@"age is %d, height is %d", age, height);
    };
    age = 20;
    height = 20;
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        test();
        block();
  }
}
  • clang编译之后的C++代码
struct __block_impl {
void *isa;
int Flags;
int Reserved;
  // 指向方法入口地址
void *FuncPtr;
};

// block对象
struct __test_block_impl_0 {
struct __block_impl impl;
struct __test_block_desc_0* Desc;
int age;
int *height;
__test_block_impl_0(void *fp, struct __test_block_desc_0 *desc, int _age, int *_height, int flags=0) : age(_age), height(_height) {
  impl.isa = &_NSConcreteStackBlock;
  impl.Flags = flags;
  impl.FuncPtr = fp;
  Desc = desc;
}
};

// test函数中,block的函数体,执行的具体方法
static void __test_block_func_0(struct __test_block_impl_0 *__cself) {
int age = __cself->age; // bound by copy
int *height = __cself->height; // bound by copy
NSLog((NSString *)&__NSConstantStringImpl__var_folders_fc_j144208d3nl9m4ql0ryqmghh0000gn_T_main_0d86ab_mi_0, age, (*height));
}

// test函数中,block的描述信息
static struct __test_block_desc_0 {
size_t reserved;
size_t Block_size;
} __test_block_desc_0_DATA = { 0, sizeof(struct __test_block_impl_0)};

void test()
{
  int age = 10;
  static int height = 10;

  block = ((void (*)())&__test_block_impl_0((void *)__test_block_func_0, &__test_block_desc_0_DATA, age, &height)); // __test_block_impl_0

  age = 20;
  height = 20;
}

int main(int argc, const char * argv[]) {
  /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool; 
      test();
      // block->FuncPtr(block); 为什么不是block->impl.FuncPtr,因为block对象首地址就是impl的地址
      ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
  }
  return 0;
}
  • 综上代码可以看出Block的C++布局
    • void *isa;
    • int Flags;
    • int Reserved;
    • void *FuncPtr; 指向block的函数体
    • struct __test_block_desc_0* Desc;(指向的内存信息包括 size_t reservedsize_t Block_size
    • 捕获变量

block的变量捕获

  • 从以上void test()函数看出
__test_block_impl_0(void *fp, struct __test_block_desc_0 *desc, int _age, int *_height, int flags=0) : age(_age), height(_height) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
  • 局部变量auto变量捕获到block,是通过值传递方式,给block对象
  • 局部变量static变量捕获到block内部,传递是一个指针,一个地址
  • 全部变量不会不会到block内部,直接访问

block的类型

  • block有三种类型,可以通过调用class方法或者isa指针查看具体类型,最终都是继承自NSBlock类型
    • NSGlobalBlock ( _NSConcreteGlobalBlock ),环境:没有访问auto变量
    • NSStackBlock ( _NSConcreteStackBlock ),环境:访问了auto变量
    • NSMallocBlock ( _NSConcreteMallocBlock ),环境: NSStackBlock 调用了Copy
image.png
image.png
    NSLog(@"%@", [block class]); // __NSGlobalBlock__
    NSLog(@"%@", [[block class] superclass]); //  NSBlock
    NSLog(@"%@", [[[block class] superclass] superclass]); // NSObject
    NSLog(@"%@", [[[[block class] superclass] superclass] superclass]); // null
  int a = 10;
  // 堆:动态分配内存,需要程序员申请申请,也需要程序员自己管理内存
  void (^block1)(void) = ^{
         NSLog(@"Hello");
  };
  int age = 10;
  void (^block2)(void) = ^{
        NSLog(@"Hello - %d", age);
   };
   NSLog(@"%@ % @ %@", [block1 class], [block2 copy], [^{
       NSLog(@"%d", age);
   } class]); // __NSGlobalBlock__ __NSMallocBlock__ __NSStackBlock__
  // Global:没有访问auto变量 - 数据段
    void (^block1)(void) = ^{
        NSLog(@"block1---------");
    };
    
    // Stack:访问了auto变量 - 栈s
    int age = 10;
    void (^block2)(void) = ^{
        NSLog(@"block2---------%d", age);
    };
    // Malloc block
    NSLog(@"%p", [block2 copy]);
 // NSStackBlock   - 堆:内存管理由h程序员销毁管理
    int age = 10;
    block = ^{
        NSLog(@"block---------%d", age);
    };
    // 在test2外部执行block,此时的输出 272632984,为什么不是10,因为test2执行完毕block对应的c++对象在栈上的内存被回收
    // 要想输出结果为仍然为10,可以调用copy方法,将block对象的内存从栈复制到堆上,堆上的内存由程序员手动管理

block的copy

  • 在ARC环境下,编译器会根据情况自动将粘上的block复制到堆上:
    • block作为函数返回值时
    • 将block赋值给__strong指针时
    • block作为Cocoa API中方法名含有usingBlock的方法参数
    • block作为GCD API的方法参数时
  • ARC下block属性的建议写法
    @property (copy, nonatomic) void (^block)(void);
    
    • 以下代码,bock访问了 auto变量,类型为StackBlock,在栈上, 但是输出的结果却是 NSMallocBlock,原因是在ARC环境下,编译器会把block对象拷贝到堆上,相当于自动调用了copy方法,如果是MRC的环境,输出的类型是NSStackBlock
    int age = 10;
     MJBlock block = ^{
        NSLog(@"---------%d", age);
      };
      NSLog(@"%@", [block class]); // ARC:__NSMallocBlock__,   MRC: __NSStackBlock__
    

block访问了对象类型的auto变量

  • 当block内部访问了对象类型的auto变量时

    • 如果block在栈上,将不会对auto变量产生强引用
    MJBlock block;
    {
      MJPerson *person = [[MJPerson alloc] init];
      person.age = 10;
      int age = 10;
      block = ^{
          NSLog(@"---------%d --%d", person.age, age);
      };
       [person release];
       //MRC person 将会在该作用域结束后释放, block的类型为__NSStackBlock__
     }
     NSLog(@"------: %@", [block class]); // __NSStackBlock__
    
    • 如果block被拷贝堆上
      • 会调用block内部的copy函数
      • copy函数内部会调用_Block_object_assign函数
      • _Block_object_assign函数会根据auto变量修辞符(__strong__weak__unsafe_unretained)做出相应的操作,形成强引用或者弱引用
      MJBlock block;
      {
          MJPerson *person = [[MJPerson alloc] init];
           person.age = 10;
           int age = 10;
              block = ^{
                  NSLog(@"---------%d --%d", person.age, age);
              };
          }
          NSLog(@"------: %@", [block class]); // __NSMallocBlock__
    
    • 将上述代码编译为C++代码
          struct __main_block_impl_0 {
             struct __block_impl impl;
             struct __main_block_desc_0* Desc;
             MJPerson *person;
             int age;
      
        __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, MJPerson *_person, int _age, int flags=0) : person(_person), age(_age) {
             impl.isa = &_NSConcreteStackBlock;
             impl.Flags = flags;
             impl.FuncPtr = fp;
             Desc = desc;
        }
       };
      
      static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
         MJPerson *person = __cself->person; // bound by copy
         int age = __cself->age; // bound by copy
         NSLog((NSString *)&__NSConstantStringImpl__var_folders_fc_j144208d3nl9m4ql0ryqmghh0000gn_T_main_8479c1_mi_0, ((int (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("age")), age);
       }
      
         static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {
           _Block_object_assign((void*)&dst->person, (void*)src->person, 3);
        }
      
         static void __main_block_dispose_0(struct __main_block_impl_0*src) {
            _Block_object_dispose((void*)src->person, 3);
       }
      // 创建一个结构体,并声明初始化变量__main_block_desc_0_DATA
        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[]) { { __AtAutoreleasePool __autoreleasepool;
           MJBlock block;
           {
               MJPerson *person = ((MJPerson *(*)(id, SEL))(void *)objc_msgSend)((id)((MJPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("MJPerson"), sel_registerName("alloc")), sel_registerName("init"));
               ((void (*)(id, SEL, int))(void *)objc_msgSend)((id)person, sel_registerName("setAge:"), 10);
               int age = 10;
               block = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, person, age, 570425344));
           }
           NSLog((NSString *)&__NSConstantStringImpl__var_folders_fc_j144208d3nl9m4ql0ryqmghh0000gn_T_main_8479c1_mi_1, ((Class (*)(id, SEL))(void *)objc_msgSend)((id)block, sel_registerName("class")));
       }
         return 0;
       }
      
    • 如果block从堆上移除
      • 会调用block内部的dispose函数
      • dispose函数内部会调用_Block_object_dispose函数
    • _Block_object_dispose函数会自动释放引用的auto变量
       static void __main_block_dispose_0(struct __main_block_impl_0*src) {
           _Block_object_dispose((void*)src->person, 3);
        }
      
image.png

OC为C++代码时,__weak问题解决

  • 在使用clang转换OC为C++代码时,可能会遇到以下问题
    • cannot create __weak reference in file using manual reference
  • 解决方案:支持ARC、指定运行时系统版本,比如
    • xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc -fobjc-arc -fobjc-runtime=ios-8.0.0 main.m

__block修饰符

  • __block可以用于 解决block内部无法修改auto变量值的问题
  • __block不能修饰全局变量,静态变量(static)
  • 编译器会将__block变量包装成一个对象
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        int no = 20;
        /**
         对__block对象直接产生强引用
         对非__block对象产生的引用取决于它的修饰符 __weak产生弱引用, __strong产生强引用
         
         struct __main_block_impl_0 {
           struct __block_impl impl;
           struct __main_block_desc_0* Desc;
           int no;
           NSObject *__weak weakObject;
           __Block_byref_age_0 *age; // by ref
             }
         */
        __block int age = 10;
        
        NSObject *object = [[NSObject alloc] init];
        __weak NSObject *weakObject = object;
        
        MJBlock block = ^{
            age = 20;
            
            NSLog(@"%d", no);
            NSLog(@"%d", age);
            NSLog(@"%p", weakObject);
        };
        
        struct __main_block_impl_0* blockImpl = (__bridge struct __main_block_impl_0*)block;
                block();
    }
    return 0;
}


typedef void (*MJBlock) (void);
struct __Block_byref_age_0 {
    void *__isa;
    __Block_byref_age_0 *__forwarding;
    int __flags;
    int __size;
    int age;
};

struct __main_block_impl_0 {
    struct __block_impl impl;
    struct __main_block_desc_0* Desc;
    int no;
    NSObject *__weak weakObject;
    __Block_byref_age_0 *age; // by ref
    
    __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, int _no, NSObject *__weak _weakObject, __Block_byref_age_0 *_age, int flags=0) : no(_no), weakObject(_weakObject), age(_age->__forwarding) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
    }
};
static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
    __Block_byref_age_0 *age = __cself->age; // bound by ref
    int no = __cself->no; // bound by copy
    NSObject *__weak weakObject = __cself->weakObject; // bound by copy
    /// age->__forwarding->age这么做的原因是为了让age始终被正确访问,block会被拷贝到堆上
    (age->__forwarding->age) = 20;
}

static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {
    
    _Block_object_assign((void*)&dst->age, (void*)src->age, 8/*BLOCK_FIELD_IS_BYREF*/);
    
    _Block_object_assign((void*)&dst->weakObject, (void*)src->weakObject, 3/*BLOCK_FIELD_IS_OBJECT*/);
}

static void __main_block_dispose_0(struct __main_block_impl_0*src) {_Block_object_dispose((void*)src->age, 8/*BLOCK_FIELD_IS_BYREF*/);_Block_object_dispose((void*)src->weakObject, 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};


int main(int argc, const char * argv[]) {
    /* @autoreleasepool */ { __AtAutoreleasePool __autoreleasepool;
        
        int no = 20;
        
        __attribute__((__blocks__(byref))) __Block_byref_age_0 age = {(void*)0,(__Block_byref_age_0 *)&age, 0, sizeof(__Block_byref_age_0), 10};
        
        NSObject *object = ((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)((NSObject *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("NSObject"), sel_registerName("alloc")), sel_registerName("init"));
        __attribute__((objc_ownership(weak))) NSObject *weakObject = object;
        
        MJBlock block = ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, no, weakObject, (__Block_byref_age_0 *)&age, 570425344));
        ((void (*)(__block_impl *))((__block_impl *)block)->FuncPtr)((__block_impl *)block);
    }
    return 0;
}

image.png
  • 可以看到在block中访问了 __block 修辞的age的变量,age在block对象中被包装 成了 __Block_byref_age_0 类的指针,指向__Block_byref_age_0对象的第一个元素的地址__isa,从__Block_byref_age_0这个类结构可以看出,里面的__forwarding指针,指向该类,可以通过该指针访问age变量
    image.png
  • __blokc修辞的auto变量(基础数据类型)的内存管理
    • 当block在栈上时,并不会对__block变量产生强引用, 在ARC环境下,编译器会将__NSStackBlock类型的block自动执行一次copy操作到堆上
    • 当block被copy到堆上时
      • 回调用block内部的copy函数
      • copy函数内部会调用_Block_object_assign函数
        -_Block_object_assig函数会对__block变量形成强引用(retain)
      • 对非__block对象产生的引用取决于它的修饰符 __weak产生弱引用, __strong产生强引用


        image.png
  • 当block从堆中移除时
    • 会调用block内部的dispose函数
    • dispose函数会调用 _Block_object_dispose函数
    • _Block_object_dispose函数会自动释放引用的_blcok变量
image.png

__block的__forwarding指针

struct __Block_byref_age_0 {
  void *__isa; // 8
  __Block_byref_age_0 *__forwarding; // 8
 int __flags; // 4
 int __size; // 4
 int age; // 0x000000010300da68
};

static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
    __Block_byref_age_0 *age = __cself->age; // bound by ref
    int no = __cself->no; // bound by copy
    NSObject *__weak weakObject = __cself->weakObject; // bound by copy
    /// age->__forwarding->age这么做的原因是为了让age始终被正确访问,block会被拷贝到堆上
     (age->__forwarding->age) = 20;
}
  • __forwarding指针指向的是自身的地址
  • 当block对象被复制到堆上时,__forwarding指针对上的结构体
  • 采用这种方式就能保证始终访问到正确位置的变量的age


    image.png

对象类型的auto变量,__block变量内存管理的总结

  • 当block在栈上时,对它们都不会产生强引用
  • 当block拷贝堆上时,都会通过copy函数来处理它们
    • __block变量(假设变量名叫做a)
      • _Block_object_assign((void*)&dst->a, (void*)src->a, 8/*BLOCK_FIELD_IS_BYREF*/);
        
    • 对象类型的auto变量
      • _Block_object_assign((void*)&dst->person, (void*)src->person, 3/*BLOCK_FIELD_IS_OBJECT*/);
        
  • 当block从堆上移除时,都会通过dispose函数来释放它们
static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {
    _Block_object_assign((void*)&dst->person, (void*)src->person, 3/*BLOCK_FIELD_IS_OBJECT*/);
    _Block_object_assign((void*)&dst->a, (void*)src->a, 8/*BLOCK_FIELD_IS_BYREF*/);
}
static void __main_block_dispose_0(struct __main_block_impl_0*src) {
    _Block_object_dispose((void*)src->person, 3/*BLOCK_FIELD_IS_OBJECT*/);
    _Block_object_dispose((void*)src->a, 8/*BLOCK_FIELD_IS_BYREF*/);
}

被被__block修饰的对象类型的内存管理情况

  • 当__block变量在栈上时,不会对指向的对象产生强引用
  • 当__block变量被copy到堆时
    • 会调用__block变量内部的copy函数
    • copy函数内部会调用_Block_object_assign函数
    • _Block_object_assign函数会根据所指向对象的修饰符(__strong、__weak、__unsafe_unretained)做出相应的操作,形成强引用(retain)或者弱引用(注意:这里仅限于ARC时会retain,MRC时不会retain)
  • 如果__block变量从堆上移除
    • 会调用__block变量内部的dispose函数
    • dispose函数内部会调用_Block_object_dispose函数
    • _Block_object_dispose函数会自动释放指向的对象(release)
    int main(int argc, const char * argv[]) {
      @autoreleasepool {
          MJPerson *person = [[MJPerson alloc] init];
          __strong MJPerson *strogPerson = person;
          __weak MJPerson *weakPerson = person;
          MJBlock block = ^{
              NSLog(@"%p - %p", strogPerson, weakPerson);
          };
          block();
      }
      return 0;
    }
    
    struct __main_block_impl_0 {
      struct __block_impl impl;
      struct __main_block_desc_0* Desc;
      MJPerson *__strong strogPerson;
      MJPerson *__weak weakPerson;
    }
    
      static void __main_block_copy_0(struct __main_block_impl_0*dst, struct __main_block_impl_0*src) {
          _Block_object_assign((void*)&dst->strogPerson, (void*)src->strogPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);
          _Block_object_assign((void*)&dst->weakPerson, (void*)src->weakPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);
    }
    
      static void __main_block_dispose_0(struct __main_block_impl_0*src) {
          _Block_object_dispose((void*)src->strogPerson, 3/*BLOCK_FIELD_IS_OBJECT*/);
          _Block_object_dispose((void*)src->weakPerson, 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};
    
    

block循环引用问题

image.png
  • 所以为了解决循环引用问题就是将其中的一个引用箭头变为虚线,产生弱引用
self.block = ^{
        NSLog(@"age is %d", self.age);
  }; 
struct __MJPerson__test_block_impl_0 {
    struct __block_impl impl;
    struct __MJPerson__test_block_desc_0* Desc;
    MJPerson *const __strong self;
};
  • 相当于堆上的block对象持有MJPerson * self指针,该指针指向MJPerson的堆空间内存,同时MJPerson对象持有该block对象,所以就造成了循环引用
image.png

解决循环引用问题

  • __weak, __unsafe_unretained解决,用weak时,相当于block对象里面的MJPerson* self变为弱指针,当指向的堆空间被销毁时,weak修饰的指针变量也会被设置为nil, 但是__unsafe_unretained修饰的指针变量不会被设置为nil,所以如果再次使用改指针变量访问时就会出现坏内存地址崩溃

    __weak typeof(self) weakSelf = self;
      self.block = ^{
          NSLog(@"age is %d", weakSelf.age);
      };
     // 使用weak时
     struct __MJPerson__test_block_impl_0 {
        struct __block_impl impl;
        struct __MJPerson__test_block_desc_0* Desc;
        MJPerson *const __weak weakSelf;
    };
    
    // 使用__unsafe_unretained时
    struct __MJPerson__test_block_impl_0 {
        struct __block_impl impl;
        struct __MJPerson__test_block_desc_0* Desc;
        MJPerson *const __unsafe_unretained weakSelf;
      };
    
  • 用__block解决,必须调用block,同时必选在block内手动设置nil, 我们知道__block对应的c++代码是会把self进行一个对象的包装,通过访问其内部的farwarding指针获取到对象内存,在block内存手动将其设置为nil,就相当于__block对象不持有self

  __block id weakSelf = self;
    self.block = ^{
        NSLog(@"age is %@", weakSelf);
        weakSelf = nil;
    };
    self.block();


struct __MJPerson__test_block_impl_0 {
    struct __block_impl impl;
    struct __MJPerson__test_block_desc_0* Desc;
    __Block_byref_weakSelf_0 *weakSelf; // by ref
};

struct __Block_byref_weakSelf_0 {
  void *__isa;
__Block_byref_weakSelf_0 *__forwarding;
 int __flags;
 int __size;
 void (*__Block_byref_id_object_copy)(void*, void*);
 void (*__Block_byref_id_object_dispose)(void*);
 __strong id weakSelf;
};
image.png

一些问题

  • block为什么要进行外部变量捕获?

  • 为何要将block转为一个类?用函数指针不行吗?

    • block本身存在一种操作那个,就是外部局部变量的捕获,需要将block相关的动效保存起来,OC都会转为C++代码,C++中能同时保存方法和变量的就是类;函数和函数指针办不到外部局部变量的捕获的功能
  • 为何static局部变量进行的是指针捕获? 值捕获不行吗?

    • 首先要明白:static局部变量具有的特性:仍然是局部变量,加上static修饰后,局部变量的生命周就变得跟全局变量一样(static局部变量的存储区域放在了全局,但是作用域仍然是函数调用栈的作用域,全局的变量在程序运行过程中,只会被声明及初始化一次,并且是在程序结束时才销毁),基于static变量的特性,是局部变量所以就会被捕获,在全局区,只能声明和初始化一次,并且随时访问到的是同一块内存空间,所以最简单的做法就是将static局部变量的内存地址保存下来,通过这个地址随时访问那一块内存空间

你可能感兴趣的:(Objective-C Block本质)