Block circular reference problem under iOS MRC //Note that this __block will copy a pointer out once the original pointer is set to nil, the pointer copied here is still a wild pointer __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); //This judgment will crash At this time, weakSelf is a wild pointer //weakSelf is a wild pointer at this time. . . Wild pointers are pointers too, right? Anyway, this wild pointer is not NULL, although the memory it points to is not of any use, // However the code doesn't know that. So executing [weakSelf doSomething]; will inevitably crash. //Note that this __block will copy a pointer out once the original pointer is set to nil, the pointer copied here is still a wild pointer // if (weakSelf) { // NSLog(@"name --> is %@", weakSelf.name); // } //malloc(22); // malloc_zon // This doesn't work. . . weakSelf is already a wild pointer and still crashes // __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"; //Note that this __block will copy a pointer out once the original pointer is set to nil, the pointer copied here is still a wild pointer __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); //This judgment will crash At this time, weakSelf is a wild pointer //weakSelf is a wild pointer at this time. . . Wild pointers are pointers too, right? Anyway, this wild pointer is not NULL, although the memory it points to is not of any use, // However the code doesn't know that. So executing [weakSelf doSomething]; will inevitably crash. //Note that this __block will copy a pointer out once the original pointer is set to nil, the pointer copied here is still a wild pointer // if (weakSelf) { // NSLog(@"name --> is %@", weakSelf.name); // } //malloc(22); // malloc_zon // This doesn't work. . . weakSelf is already a wild pointer and still crashes // __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