Block底层原理四-循环引用分析

循环引用非常常见,我们来分析一下为什么会循环引用


#import 
#import "WKPerson.h"

typedef void (^WKBlock)(void);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        WKPerson *person = [[WKPerson alloc] init];
        person.age = 20;
        person.block = ^{
            
        };
    }
     NSLog(@"---------");
    return 0;
}

//WKPerson.h
#import 

typedef void (^WKBlock) (void);

@interface WKPerson : NSObject

@property (copy, nonatomic) WKBlock block;
@property (assign, nonatomic) int age;


@end

//WKPerson.m
#import "WKPerson.h"

@implementation WKPerson

- (void)dealloc
{
    //    [super dealloc];
    NSLog(@"%s", __func__);
}

@end

2018-08-21 10:48:02.483714+0800 mhhhh[16581:274772] -[WKPerson dealloc]
2018-08-21 10:48:02.484591+0800 mhhhh[16581:274772] ---------
Program ended with exit code: 0

  • 我们看到了WKPerson已经被释放掉了dealloc方法执行了
  • 接下来我要加一句代码,就是这句代码导致循环引用
#import 
#import "WKPerson.h"

typedef void (^WKBlock)(void);

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        WKPerson *person = [[WKPerson alloc] init];
        person.age = 20;
        person.block = ^{
            NSLog(@"%d",person.age);
        };
    }
     NSLog(@"---------");
    return 0;
}

2018-08-21 10:52:00.748869+0800 mhhhh[16622:281567] ---------
Program ended with exit code: 0

我们来分析一下 为什么加了NSLog后会循环引用

 xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m
//main.m
struct __main_block_impl_0 {
  struct __block_impl impl;
  struct __main_block_desc_0* Desc;
  WKPerson *person;
  __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, WKPerson *_person, int flags=0) : person(_person) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

我们来看看WKPerson类

 xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc WKPerson.m
struct WKPerson_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    int _age;
    WKBlock _block;
};

struct NSObject_IMPL {
    Class isa;
};

粗略的说 NSObject内部

struct objc_class{
      Class isa;
      Class superclass;
      cache_t cache;//方法缓存
      class_data_bits_t bits;//用于具体方法信息
}

struct class_rw_t{
        uint32_t flags;
        const class_ro_t *ro;
        property_list_t *properties //属性列表
}

class_ro_t 属性列表就放着instance 成员变量列表也就是age属性
后面我会专门写一篇NSObject里面的内存 以及isa的指向

接下来我们分析一下 为啥会循环引用
1.person->block 调用了block
2.block内部调用了person.age
3.person isa存着block的内存地址,这个内存地址就是person->block
4.所以当person作用域结束了,还有一根强指针指向他,person不能释放

图花的比较丑


image.png

如果这时候加上__weak

  • person->block 调用了block(强指针)
  • block内部调用了person.age
  • person isa存着block的内存地址,这个内存地址就是person->block (weak)弱指针,也就是下面的那根线变成了虚线,所以当persion作用域结束后,就会调用dispose函数,进行释放,释放后引用计数器-1等于0 所以就能够释放。

你可能感兴趣的:(Block底层原理四-循环引用分析)