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)存并未有什么鳥(niǎo)用,
//然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。
//注意此__block會(huì)復(fù)制一份指針出來(lái) 一次原始的指針如果置為nil的話,此處復(fù)制出來(lái)的指針還是野指針
// if (weakSelf) {
// NSLog(@"name --> is %@", weakSelf.name);
// }
//malloc(22);
// malloc_zon
//這并沒(méi)有什么卵用。。。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)存并未有什么鳥(niǎo)用,
//然而代碼并不知道。所以 執(zhí)行[weakSelf doSomething]; 必然閃退。
//注意此__block會(huì)復(fù)制一份指針出來(lái) 一次原始的指針如果置為nil的話,此處復(fù)制出來(lái)的指針還是野指針
// if (weakSelf) {
// NSLog(@"name --> is %@", weakSelf.name);
// }
//malloc(22);
// malloc_zon
//這并沒(méi)有什么卵用。。。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實(shí)現(xiàn)基于CMPedometer的計(jì)步器
這篇文章主要為大家詳細(xì)介紹了IOS實(shí)現(xiàn)基于CMPedometer的計(jì)步器,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06
詳解iOS開(kāi)發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法
這篇文章主要介紹了iOS開(kāi)發(fā)中使用storyboard創(chuàng)建導(dǎo)航控制器的方法,包括對(duì)控制器聲明周期的控制介紹,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-01-01
IOS9.0 LaunchScreen.StroyBoard自定義啟動(dòng)圖片詳解
這篇文章主要介紹了IOS9.0 LaunchScreen.StroyBoard自定義啟動(dòng)圖片詳解的相關(guān)資料,需要的朋友可以參考下2017-02-02
實(shí)例講解iOS應(yīng)用的設(shè)計(jì)模式開(kāi)發(fā)中的Visitor訪問(wèn)者模式
這篇文章主要介紹了iOS應(yīng)用的設(shè)計(jì)模式開(kāi)發(fā)中的Visitor訪問(wèn)者模式的實(shí)例,示例代碼為傳統(tǒng)的Objective-C,需要的朋友可以參考下2016-03-03
iOS實(shí)現(xiàn)漸變按鈕Gradient Button的方法示例
這篇文章主要給大家介紹了關(guān)于iOS實(shí)現(xiàn)漸變按鈕Gradient Button的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)各位iOS開(kāi)發(fā)者們具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08

