iOS MRC 下 block 循環(huán)引用問題實例講解
更新時間:2017年12月12日 10:37:25 作者:jeffasd
本文通過文字說明加代碼的形式給大家介紹了iOS MRC 下 block 循環(huán)引用問題,非常不錯,具有參考借鑒價值,需要的朋友參考下吧
下面一段代碼給大家介紹iOS MRC 下 block 循環(huán)引用問題
//注意此__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,雖然它指向的內(nèi)存并未有什么鳥用,
//然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。
//注意此__block會復制一份指針出來 一次原始的指針如果置為nil的話,此處復制出來的指針還是野指針
// 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會復制一份指針出來 一次原始的指針如果置為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,雖然它指向的內(nèi)存并未有什么鳥用,
//然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。
//注意此__block會復制一份指針出來 一次原始的指針如果置為nil的話,此處復制出來的指針還是野指針
// 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)引用問題,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS利用Label實現(xiàn)的簡單高性能標簽TagView
這篇文章主要給大家介紹了關(guān)于iOS利用Label實現(xiàn)的簡單高性能標簽TagView的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-03-03
深入詳解Objective-C中的@Synchronized關(guān)鍵字
這篇文章主要為大家介紹了深入詳解Objective-C中的@Synchronized關(guān)鍵字,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-03-03
詳解IOS UITableViewCell 的 imageView大小更改
這篇文章主要介紹了詳解IOS UITableViewCell 的 imageView大小更改的相關(guān)資料,需要的朋友可以參考下2017-07-07
iOS10添加本地推送(Local Notification)實例
這篇文章主要為大家詳細介紹了iOS10添加本地推送(Local Notification)實例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-09-09

