首页 > 其他 > 详细

彻底分析block中的循环引用

时间:2015-05-11 09:04:24      阅读:355      评论:0      收藏:0      [点我收藏+]

我做了一个被push到的controller,pop的时候,controller会释放,解析详见注释,欢迎批评指正!

#import "AnimaViewController.h"

@interface AnimaViewController ()
@property (nonatomic, strong) UIView *view1;
@property (nonatomic, strong) UIView *view2;
@property (nonatomic, copy) void (^block)(int);//self持有了block
@property (nonatomic, strong) UIButton *btn;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation AnimaViewController

__weak AnimaViewController *weakSelf;//用弱引用来跟踪self的释放情况。

- (void)viewDidLoad {
    weakSelf = self;//指向self的弱引用。
    
    [super viewDidLoad];
    self.view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 100, 90, 90)];
    self.view1.backgroundColor = [UIColor orangeColor];
    [self.view addSubview:self.view1];
    
    self.view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 300, 90, 90)];
    self.view2.backgroundColor = [UIColor blueColor];
    [self.view addSubview:self.view2];
    
    self.btn = [[UIButton alloc]init];
    self.btn.backgroundColor = [UIColor blackColor];;
    [self.btn setFrame:CGRectMake(100, 100, 80, 80)];
    [self.btn addTarget:self action:@selector(tapAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.btn];
    
    //系统有时候会提示你可能导致循环引用,但会有漏掉的情况,比如strong变量定义成全局变量的时候。
    __weak AnimaViewController *weakSelfLocal = self;//弱引用,引用计数不会增加。
    self.block = ^ (int num){
        __strong AnimaViewController *strongSelf = weakSelfLocal;//增加了对self的引用计数。
        [strongSelf.btn setTitle:[NSString stringWithFormat:@"%d", num] forState:UIControlStateNormal];
        [strongSelf.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        //此后strongSelf变量作为局部strong变量,被系统发送了release消息(编译时就已插入release),所以strongSelf已经不构成对self的引用。如果将__weak AnimaViewController *weakSelfLocal定义为全局变量,那么此处strongSelf不会被release,所以仍然保持对self的强引用,则会导致循环引用。此处定义strong变量的用意是防止self被释放,导致此处的操作失效。
    };
    
    self.block(100);
    
    NSLog(@"%@", weakSelf);//此处self没有被释放,所以对它的弱引用变量weakSelf不为null。
    
    //关于注册通知中的block:The block is copied by the notification center and (the copy) held until the observer registration is removed.可见block中不能持有self的强引用,否则只有解除注册的时候,才能释放对self的持有,而self只有释放的时候才会在dealloc中解除通知的注册,则导致循环引用。所以要用弱引用代替self。
    [[NSNotificationCenter defaultCenter]addObserverForName:@"blockretaincycle" object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
        [weakSelfLocal.btn setTitle:[NSString stringWithFormat:@"%d", 888] forState:UIControlStateNormal];
        [weakSelfLocal.btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }];
    
    //runloop对timer强引用,timer的target又强引用了self,self又强引用了timer,所以会循环引用,不会调用dealloc。
    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.0
                                     target:self
                                   selector:@selector(timerMethod)
                                   userInfo:nil
                                    repeats:YES];
}

- (void)timerMethod{
    NSLog(@"%@", [NSDate date]);
}

- (void)tapAction:(UIButton *)sender{
    //很多系统自带的block里,可以直接用self,不会循环引用。不清楚的可以看苹果官方文档。
    [UIView animateWithDuration:1.0 animations:^{
        [self.view1 setFrame:CGRectMake(100, 400, 70, 70)];
    } completion:^(BOOL finished) {
        [self.view1 setFrame:CGRectMake(100, 400, 70, 70)];
    }];
}

- (void)dealloc{
    NSLog(@"%@", weakSelf);//weakSelf是全局的,而此处打印为(null),说明self已经被释放。如果导致循环引用,则不会调用这个方法。
    [[NSNotificationCenter defaultCenter]removeObserver:self name:@"blockretaincycle" object:nil];
//    [self.timer invalidate];//写在这里没用,不会调用的
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.timer invalidate];//写在这里,确保退出页面的时候,timer失效,不会对self强引用,self可以销毁了。
}

@end

彻底分析block中的循环引用

原文:http://blog.csdn.net/ioszhuang2015/article/details/45624037

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!