iOS MRC 下 block 循環(huán)引用問(wèn)題實(shí)例講解
下面一段代碼給大家介紹iOS MRC 下 block 循環(huán)引用問(wè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)引用問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
iOS利用Label實(shí)現(xiàn)的簡(jiǎn)單高性能標(biāo)簽TagView
這篇文章主要給大家介紹了關(guān)于iOS利用Label實(shí)現(xiàn)的簡(jiǎn)單高性能標(biāo)簽TagView的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2018-03-03
深入詳解Objective-C中的@Synchronized關(guān)鍵字
這篇文章主要為大家介紹了深入詳解Objective-C中的@Synchronized關(guān)鍵字,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-03-03
IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解
這篇文章主要介紹了IOS 創(chuàng)建并發(fā)線程的實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-07-07
詳解IOS UITableViewCell 的 imageView大小更改
這篇文章主要介紹了詳解IOS UITableViewCell 的 imageView大小更改的相關(guān)資料,需要的朋友可以參考下2017-07-07
iOS10添加本地推送(Local Notification)實(shí)例
這篇文章主要為大家詳細(xì)介紹了iOS10添加本地推送(Local Notification)實(shí)例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09

