iOS 內(nèi)存泄漏檢測(cè) -MLeaksFinder

MLeaksFinder

直接CocoaPods導(dǎo)入MLeaksFinder。

pod 'MLeaksFinder'

Pods 目錄成功導(dǎo)入 FBRetainCycleDetector 和MLeaksFinder 之后無需修改任何程序代碼 在模擬器 或真機(jī)上操作程序即可,

--代碼如下

//ViewController.m
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    [self.navigationController pushViewController:secondVC animated:YES];
}
//SecondViewController.m

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor redColor];
    UIBarButtonItem *leftBtn =[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleDone target:self action:@selector(mySiteBack)];
 
    self.navigationItem.leftBarButtonItem = leftBtn;
    
    self.selected = ^(NSString *second){
        _second =@"2";
    };
}
#pragma mark--返回按鈕--
- (void)mySiteBack{
    [self.navigationController popViewControllerAnimated:YES] ;
}

//點(diǎn)擊返回

MLeaksFinder 提示沒有走dealloc的頁面存在可能內(nèi)存泄漏


IMG_0892.PNG

點(diǎn)擊 Retain Cycle 由FBRetainCycleDetector 檢測(cè)出引起循環(huán)引用的屬性或?qū)ο?/p>

IMG_0893.PNG

__weak修飾解決循環(huán)引用

 __weak typeof(self)Myself = self;
    self.selected = ^(NSString *second){
      Myself.second =@"2";
    };

__block修飾并不能避免循環(huán)引用

  __block typeof(self)Myself = self;

  self.selected = ^(NSString *second){
      Myself.second =@"2";
    };

結(jié)果如下


IMG_0895.PNG
IMG_0896.PNG

SecondViewController 并沒有走dealloc方法
"http://www.lxweimin.com/p/d73772dc36a8"__block與__weak的真正區(qū)別

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容