iOS MRC 下 block 循环引用问题实例讲解
程序员文章站
2023-12-19 21:49:52
下面一段代码给大家介绍ios mrc 下 block 循环引用问题  ...
下面一段代码给大家介绍ios mrc 下 block 循环引用问题
//注意此__block会复制一份指针出来 一次原始的指针如果置为nil的话,此处复制出来的指针还是野指针 __block __typeof(self)weakself = self; //__weak __typeof(self)weakself = self; //__weak person *weakself = self; void (^block)(void) = ^(void){ //nslog(@"name --> is %@", self.name); //nslog(@"name --> is %@", weakself.name); //这样判断会crash 此时weakself为野指针 //weakself 这时候是个野指针。。。野指针也是指针对吧?反正,这个野指针并不为null,虽然它指向的内存并未有什么鸟用, //然而代码并不知道。所以 执行[weakself dosomething]; 必然闪退。 //注意此__block会复制一份指针出来 一次原始的指针如果置为nil的话,此处复制出来的指针还是野指针 // if (weakself) { // nslog(@"name --> is %@", weakself.name); // } //malloc(22); // malloc_zon //这并没有什么卵用。。。weakself 已经是野指针 照样crash // __strong __typeof(weakself) strongself = weakself; // if (weakself) { // nslog(@"name --> is %@", strongself.name); // } if (malloc_zone_from_ptr(weakself)) { nslog(@"name --> is %@", weakself.name); }
// // viewcontroller.m // test_mrc_block_self_01 // // created by jeffasd on 2017/12/1. // copyright © 2017年 jeffasd. all rights reserved. // #import "viewcontroller.h" #import "person.h" @interface viewcontroller () @property (nonatomic, copy) nsstring *name; @end @implementation viewcontroller - (void)viewdidload { [super viewdidload]; self.view.backgroundcolor = [uicolor whitecolor]; self.name = @"xiaoming"; } - (void)touchesbegan:(nsset<uitouch *> *)touches withevent:(uievent *)event{ self.view.backgroundcolor = [uicolor cyancolor]; // void (^block)(void) = ^(void){ // nslog(@"name --> is %@", self.name); // }; // // // // for (int i = 0; i < 30; i++) { // block(); // } person *xiaoming = [[person alloc] init]; //[xiaoming retain]; [xiaoming release]; // xiaoming = nil; xiaoming = null; } - (void)didreceivememorywarning { [super didreceivememorywarning]; // dispose of any resources that can be recreated. } @end
// // person.m // test_mrc_block_self_01 // // created by jeffasd on 2017/12/1. // copyright © 2017年 jeffasd. all rights reserved. // #import "person.h" #include <malloc/malloc.h> @interface person () @property (nonatomic, copy) nsstring *name; @end @implementation person - (instancetype)init{ if (self = [super init]) { self.name = @"xiaoming"; //注意此__block会复制一份指针出来 一次原始的指针如果置为nil的话,此处复制出来的指针还是野指针 __block __typeof(self)weakself = self; //__weak __typeof(self)weakself = self; //__weak person *weakself = self; void (^block)(void) = ^(void){ //nslog(@"name --> is %@", self.name); //nslog(@"name --> is %@", weakself.name); //这样判断会crash 此时weakself为野指针 //weakself 这时候是个野指针。。。野指针也是指针对吧?反正,这个野指针并不为null,虽然它指向的内存并未有什么鸟用, //然而代码并不知道。所以 执行[weakself dosomething]; 必然闪退。 //注意此__block会复制一份指针出来 一次原始的指针如果置为nil的话,此处复制出来的指针还是野指针 // if (weakself) { // nslog(@"name --> is %@", weakself.name); // } //malloc(22); // malloc_zon //这并没有什么卵用。。。weakself 已经是野指针 照样crash // __strong __typeof(weakself) strongself = weakself; // if (weakself) { // nslog(@"name --> is %@", strongself.name); // } if (malloc_zone_from_ptr(weakself)) { nslog(@"name --> is %@", weakself.name); } }; for (int i = 0; i < 300; i++) { // dispatch_async(dispatch_get_global_queue(dispatch_queue_priority_default, 0), ^{ // block(); // }); dispatch_async(dispatch_get_main_queue(), ^{ block(); }); } } return self; } @end
总结
以上所述是小编给大家介绍的ios mrc 下 block 循环引用问题,希望对大家有所帮助