block-循环引用

作者: RM_乾笙 | 来源:发表于2018-07-04 15:07 被阅读28次

    block的循环引用,在日常开发中,我们常常遇到,但是可能部分新人还不太了解为何会循环引用,到底是如何循环引用理解得不够透彻,并且在ARC环境下只知道用__weakSelf去解决,但也不知道原因,现在我们来剖析一下,循环引用的的底层原理。看看下面一段常见的代码:

    循环引用原因分析

    #import <Foundation/Foundation.h>
    #import "RMPerson.h"
    
    int main(int argc, const char * argv[]) {
        @autoreleasepool {
            // insert code here...
            RMPerson *person = [[RMPerson alloc] init];
            person.age = 20;
            person.block = ^{
                NSLog(@"age is %d",person.age);
            };
            person.block();
        }
        NSLog(@"----------------");
        return 0;
    }
    
    ---------------RMPerson.h----------------
    #import <Foundation/Foundation.h>
    @interface RMPerson : NSObject
    @property (nonatomic, assign) int age;
    @property (nonatomic, copy) void (^block)(void);
    @end
    ---------------RMPerson.h----------------
    #import "RMPerson.h"
    @implementation RMPerson
    @end
    
    // 控制台打印
    2018-07-04 11:27:23.129229+0800 block-循环引用[26623:2507647] age is 20
    2018-07-04 11:27:23.129421+0800 block-循环引用[26623:2507647] -----------------
    Program ended with exit code: 0
    

    从上面的代码可以得出,block调用完后,person都没用释放,NSLog(@"----------------");打印完了,person也还没释放,说明person引用计数器不为0。
    用clang 命令将 这段代码转换成C++代码(xcrun -sdk iphoneos clang -arch arm64 -rewrite-objc main.m)如下:

    struct __main_block_impl_0 {
      struct __block_impl impl;
      struct __main_block_desc_0* Desc;
      RMPerson *__strong person;
      __main_block_impl_0(void *fp, struct __main_block_desc_0 *desc, RMPerson *__strong _person, int flags=0) : person(_person) {
        impl.isa = &_NSConcreteStackBlock;
        impl.Flags = flags;
        impl.FuncPtr = fp;
        Desc = desc;
      }
    };
    
    static void __main_block_func_0(struct __main_block_impl_0 *__cself) {
      RMPerson *__strong person = __cself->person; // bound by copy
    
                NSLog((NSString *)&__NSConstantStringImpl__var_folders_5l_0xn052bn6dgb9z7pfk8bbg740000gn_T_main_d61985_mi_0,((int (*)(id, SEL))(void *)objc_msgSend)((id)person, sel_registerName("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/*BLOCK_FIELD_IS_OBJECT*/);
    
    }
    
    static void __main_block_dispose_0(struct __main_block_impl_0*src) {
        
        _Block_object_dispose((void*)src->person, 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; 
    
    
            RMPerson *person = ((RMPerson *(*)(id, SEL))(void *)objc_msgSend)((id)((RMPerson *(*)(id, SEL))(void *)objc_msgSend)((id)objc_getClass("RMPerson"), sel_registerName("alloc")), sel_registerName("init"));
            ((void (*)(id, SEL, void (*)()))(void *)objc_msgSend)((id)person, sel_registerName("setBlock:"), ((void (*)())&__main_block_impl_0((void *)__main_block_func_0, &__main_block_desc_0_DATA, person, 570425344)));
            ((void (*(*)(id, SEL))())(void *)objc_msgSend)((id)person, sel_registerName("block"))();
        }
        return 0;
    }
    static struct IMAGE_INFO { unsigned version; unsigned flag; } _OBJC_IMAGE_INFO = { 0, 2 };
    

    这段代码,我们再熟悉不过了,因为从之前block的章节中,几乎每一个相关的block小问题,都会上底层代码来研究其原理。循环引用的原因就是,1.person里的属性block强引用block,因block又捕获了person,block在copy的时候,_Block_object_assign根据person是strong,还是__weak,来对person的引用计数器做是否+1的处理,而此处的person是strong修饰的,所以block又强引用person,person的引用计数器+1。所以2.block又强引用了person
    用图片表示就是下面:

    block循环引用

    解决循环引用问题

    1、用__weak、__unsafe_unretained解决
    //  __weak
    RMPerson *person = [[RMPerson alloc] init];
    person.age = 20;
    __weak typeof(person) weakPerson = person;
    person.block = ^{
        NSLog(@"age is %d",weakPerson.age);
    };
    person.block();
    
    • 如上使用__weak可解决循环引用,也是开发中最常用最安全的方法,使用__weak,block捕获person的进去时,block不会强引用person,而是对对象弱引用,如下图
      弱引用
    // __unsafe_unretained
    RMPerson *person = [[RMPerson alloc] init];
    person.age = 20;
    __unsafe_unretained typeof(person) weakPerson = person;
    person.block = ^{
        NSLog(@"age is %d",weakPerson.age);
    };
    person.block();
    
    • 使用__unsafe_unretained也可以解决,__unsafe_unretained顾名思义就是不安全、不retained的意思,__unsafe_unretained__weak相比较主要区别是在于,当person释放的时候,block也随之销毁,但是在__unsafe__unretained修饰下的weakPerson会依然指向之前的内存空间,此时weakPerson访问的就是"僵尸对象",所以就是不安全。
    总结:
    • __unsafe_unretained: 不会对对象进行retain,当对象销毁时,会依然指向之前的内存空间(野指针)
    • __weak: 不会对对象进行retain,当对象销毁时,会自动指向nil

    相比之下,建议开发中使用__weak__weak更安全,更有效

    2、用__block解决(必须调用block)
    RMPerson *person = [[RMPerson alloc] init];
    __block weakPerson = person;
    self.block = ^ {
         NSLog(@"person : %@",weakPerson);
         weakPerson = nil;
    };
    self.block();
    

    __block修饰的变量,会被包装成一个对象,也持有了person对象,如下面一张图


    所以,要解决循环引用,就把__block变量持有的person对象的指针置为nil后,就可以解决,也因此必须要调用block,才能将__block变量置空如下图:

    总结:循环引用是因为,对象强引用了blcok,block内部也强引用了捕获进去的对象,相互引用无法释放。解决循环引用,1、用__weak、__unsafe_unretained解决,2、用__block解决(必须调用block).


    以上分析得解决循环引用的都是在ARC环境下的,现在也简单下分析MRC环境下是如何避免循环引用。
    解决循环引用(MRC环境)

    1、用__unsafe_unretained解决(MRC环境下是不支持__weak弱指针的)

    // __unsafe_unretained
    RMPerson *person = [[RMPerson alloc] init];
    person.age = 20;
    __unsafe_unretained typeof(person) weakPerson = person;
    person.block = ^{
        NSLog(@"age is %d",weakPerson.age);
    };
    person.block();
    

    2、用__block解决

    RMPerson *person = [[RMPerson alloc] init];
    __block weakPerson = person;
    self.block = ^ {
         NSLog(@"person : %@",weakPerson);
    };
    

    在MRC环境下,__block不会对person强引用,所以不会存在循环引用。

    相关文章

      网友评论

        本文标题:block-循环引用

        本文链接:https://www.haomeiwen.com/subject/ynzsuftx.html