欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

iOS MRC 下 block 循環(huán)引用問題實(shí)例講解

 更新時(shí)間:2017年12月12日 10:37:25   作者:jeffasd  
本文通過文字說明加代碼的形式給大家介紹了iOS MRC 下 block 循環(huán)引用問題,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友參考下吧

下面一段代碼給大家介紹iOS MRC 下 block 循環(huán)引用問題            

  //注意此__block會(huì)復(fù)制一份指針出來(lái) 一次原始的指針如果置為nil的話,此處復(fù)制出來(lái)的指針還是野指針 
    __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); 
      //這樣判斷會(huì)crash 此時(shí)weakSelf為野指針 
      //weakSelf 這時(shí)候是個(gè)野指針。。。野指針也是指針對(duì)吧?反正,這個(gè)野指針并不為NULL,雖然它指向的內(nèi)存并未有什么鳥用, 
      //然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。 
      //注意此__block會(huì)復(fù)制一份指針出來(lái) 一次原始的指針如果置為nil的話,此處復(fù)制出來(lái)的指針還是野指針 
//      if (weakSelf) { 
//        NSLog(@"name --> is %@", weakSelf.name); 
//      } 
      //malloc(22); 
//      malloc_zon 
      //這并沒有什么卵用。。。weakSelf 已經(jīng)是野指針 照樣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會(huì)復(fù)制一份指針出來(lái) 一次原始的指針如果置為nil的話,此處復(fù)制出來(lái)的指針還是野指針 
    __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); 
      //這樣判斷會(huì)crash 此時(shí)weakSelf為野指針 
      //weakSelf 這時(shí)候是個(gè)野指針。。。野指針也是指針對(duì)吧?反正,這個(gè)野指針并不為NULL,雖然它指向的內(nèi)存并未有什么鳥用, 
      //然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。 
      //注意此__block會(huì)復(fù)制一份指針出來(lái) 一次原始的指針如果置為nil的話,此處復(fù)制出來(lái)的指針還是野指針 
//      if (weakSelf) { 
//        NSLog(@"name --> is %@", weakSelf.name); 
//      } 
      //malloc(22); 
//      malloc_zon 
      //這并沒有什么卵用。。。weakSelf 已經(jīng)是野指針 照樣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 

總結(jié)

以上所述是小編給大家介紹的iOS MRC 下 block 循環(huán)引用問題,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

最新評(píng)論