Block由浅入深(6):循环引用

循环引用原因

我们都知道,使用Block的时候不小心就会造成循环引用的问题,那么为什么会出现循环引用呢,本文我们同样使用前文所有的工具和已经得到的原理来分析这个问题。

ARC环境下的循环引用

我们有如下代码,有经验的同学可以一眼就看出来这段代码有循环引用问题:

typedef void(^blk_t)(void);

@interface MyObject : NSObject
{
    blk_t blk;
}

@end

@implementation MyObject

- (instancetype)init {
    if (self = [super init]) {
        blk = ^{NSLog(@"self = %@", self);};
    }

    return self;
}

@end

那么为什么会发生循环引用呢,同样使用clang工具将以上代码转换成C++代码,我们只提取了重要的部分并做了简化来说明:

使用命令clang -rewrite-objc -Wno-deprecated-declarations -fobjc-arc $file.m-fobjc-arc明确将使用ARC。

struct MyObject_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    __strong blk_t blk;
};

struct __MyObject__init_block_impl_0 {
  struct __block_impl impl;
  struct __MyObject__init_block_desc_0* Desc;
  MyObject *__strong self;
  __MyObject__init_block_impl_0(void *fp, struct __MyObject__init_block_desc_0 *desc, MyObject *__strong _self, int flags=0) : self(_self) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static instancetype _I_MyObject_init(MyObject * self, SEL _cmd) {
    if (self = [super init]) {
        blk = &__MyObject__init_block_impl_0((void *)__MyObject__init_block_func_0, &__MyObject__init_block_desc_0_DATA, self, 570425344));
    }

    return self;
}

上述代码中,MyObject_IMPL相当于是MyObject的成员变量的实现,__MyObject__init_block_impl_0是代码里block的实现。我们可以看到,MyObject_IMPL中的blk变量是__strong修饰的,所以它会持有blk,同样block的实现中,self也是__strong修饰的,所以blk也会持有self,这样就造成了循环引用。

MRC环境下的循环引用

我们使用同样的例子来说明MRC环境下的循环引用问题,不过为了让代码能够正常运行,需要做一些修改:

typedef void(^blk_t)(void);

@interface MyObject : NSObject
{
    blk_t blk;
}

@end

@implementation MyObject

- (instancetype)init {
    if (self = [super init]) {
        blk = [^{NSLog(@"self = %@", self);} copy];
    }

    return self;
}

- (void)dealloc {
    [super dealloc];
}

@end

我们同样转化为C++代码,主要代码如下:

使用命令clang -rewrite-objc -Wno-deprecated-declarations -fno-objc-arc $file.m-fno-objc-arc指定不使用ARC。

struct MyObject_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    blk_t blk;
};

struct __MyObject__init_block_impl_0 {
  struct __block_impl impl;
  struct __MyObject__init_block_desc_0* Desc;
  MyObject *self;
  __MyObject__init_block_impl_0(void *fp, struct __MyObject__init_block_desc_0 *desc, MyObject *_self, int flags=0) : self(_self) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __MyObject__init_block_copy_0(struct __MyObject__init_block_impl_0*dst, struct __MyObject__init_block_impl_0*src) {_Block_object_assign((void*)&dst->self, (void*)src->self, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static void __MyObject__init_block_dispose_0(struct __MyObject__init_block_impl_0*src) {_Block_object_dispose((void*)src->self, 3/*BLOCK_FIELD_IS_OBJECT*/);}

static struct __MyObject__init_block_desc_0 {
  size_t reserved;
  size_t Block_size;
  void (*copy)(struct __MyObject__init_block_impl_0*, struct __MyObject__init_block_impl_0*);
  void (*dispose)(struct __MyObject__init_block_impl_0*);
} __MyObject__init_block_desc_0_DATA = { 0, sizeof(struct __MyObject__init_block_impl_0), __MyObject__init_block_copy_0, __MyObject__init_block_dispose_0};

同样,MyObject_IMPL相当于是MyObject的成员变量的实现,__MyObject__init_block_impl_0是代码里block的实现,各自的实现中已经没有了__strong修饰符了。
但是在MRC下,我们对Block执行了copy,它会调用__MyObject__init_block_copy_0方法,而该方法会调用_Block_object_assign方法,我们可以阅读该方法对源代码,相关部分摘录如下:

void _Block_object_assign(void *destAddr, const void *object, const int flags)
{
  // ......
    if (IS_SET(flags, BLOCK_FIELD_IS_OBJECT) &&
          !IS_SET(flags, BLOCK_BYREF_CALLER))
    {
        id src = (id)object;
        void **dst = destAddr;
        *dst = src;
        if (!isGCEnabled)
        {
            *dst = objc_retain(src);
        }
    }
}

所以在执行完copy之后,会导致self的引用计数加1,但是代码中却没有对应的释放引用计数的代码,所以会导致MyObject对象无法释放,同样Block也无法释放。

破解循环引用的原理

ARC下破解循环引用的原理

我们知道在ARC下破解循环引用的方法是使用__weak修饰符,如下代码:

__weak MyObject *tmp = self;
blk = ^{NSLog(@"self = %@", tmp);};

修改后,转换的主要代码如下:

因为使用了__weak,需要在转换的时候指定objc-runtime参数:-fobjc-runtime=ios-11.0.0

struct MyObject_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    __strong blk_t blk;
};

struct __MyObject__init_block_impl_0 {
  struct __block_impl impl;
  struct __MyObject__init_block_desc_0* Desc;
  MyObject *__weak tmp;
  __MyObject__init_block_impl_0(void *fp, struct __MyObject__init_block_desc_0 *desc, MyObject *__weak _tmp, int flags=0) : tmp(_tmp) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

可以看到,在block里对self的引用已经变成了__weak的了,这样就不会再有循环引用了。

MRC下破解循环引用的原理

在MRC环境下,我们不能使用__weak修饰符来解决这个问题了。也许聪明的看官已经知道了一种破解方法:既然是因为copy导致的引用计数加1,那么在Block体内做一次release就好了,如下:

blk = [^{
  NSLog(@"self = %@", self);
  [self release];
} copy];

经过实验,这种方法确实能解决循环引用的问题,但是这种编码方式不太符合苹果的内存管理原则(简单来说,retain和release是对应的),所以代码看起来会有点怪异。
解决MRC下循环引用的关键在于在对Block执行copy的时候不增加self的引用计数,我们有另外一种方案:

__block MyObject *tmp = self;
blk = [^{NSLog(@"self = %@", tmp);} copy];

经过转化后的主要代码如下:

struct MyObject_IMPL {
    struct NSObject_IMPL NSObject_IVARS;
    blk_t blk;
};

struct __Block_byref_tmp_0 {
  void *__isa;
__Block_byref_tmp_0 *__forwarding;
 int __flags;
 int __size;
 void (*__Block_byref_id_object_copy)(void*, void*);
 void (*__Block_byref_id_object_dispose)(void*);
 MyObject *tmp;
};

struct __MyObject__init_block_impl_0 {
  struct __block_impl impl;
  struct __MyObject__init_block_desc_0* Desc;
  __Block_byref_tmp_0 *tmp; // by ref
  __MyObject__init_block_impl_0(void *fp, struct __MyObject__init_block_desc_0 *desc, __Block_byref_tmp_0 *_tmp, int flags=0) : tmp(_tmp->__forwarding) {
    impl.isa = &_NSConcreteStackBlock;
    impl.Flags = flags;
    impl.FuncPtr = fp;
    Desc = desc;
  }
};

static void __MyObject__init_block_copy_0(struct __MyObject__init_block_impl_0*dst, struct __MyObject__init_block_impl_0*src) {_Block_object_assign((void*)&dst->tmp, (void*)src->tmp, 8/*BLOCK_FIELD_IS_BYREF*/);}

对Block执行copy的时候会调用__MyObject__init_block_copy_0方法,该方法会调用_Block_object_assign方法,这个方法会做如下操作:

void _Block_object_assign(void *destAddr, const void *object, const int flags)
{
    if (IS_SET(flags, BLOCK_FIELD_IS_BYREF))
    {
        struct block_byref_obj *src = (struct block_byref_obj *)object;
        struct block_byref_obj **dst = destAddr;
        src = src->forwarding;

        if ((src->flags & BLOCK_REFCOUNT_MASK) == 0)
        {
            // ...
        }
        else
        {
            *dst = (struct block_byref_obj*)src;
            increment24(&(*dst)->flags);
        }
    }
}

由以上源代码可以得知,在copy的时候没有对self执行retain,所以不会增加self的引用技术。

总结

本文我们分别分析了MRC和ARC环境下循环引用产生的原因和解决原理,在MRC下一般推荐使用__block,在ARC下推荐使用__weak,对于self变量,可以使用如下的宏定义:

#ifndef    weakify
    #if __has_feature(objc_arc)
        #define weakify autoreleasepool{} __weak __typeof__(self) weakRef = self;
    #else
        #define weakify autoreleasepool{} __block __typeof__(self) blockRef = self;
    #endif
#endif

使用语法是@weakify,在ARC下会生成weakRef变量,在MRC下会生成blockRef变量。

你可能感兴趣的:(Block由浅入深(6):循环引用)