iOS Viewcontroller及其他类对dealloc方法调用的理解
正常情况下,ViewController pop/dismiss之后会被推出栈,进入ViewController的dealloc方法。如果没有走到dealloc方法中,则表明没有被释放,有内存泄露的现象。一般造成内存泄漏的现象分为三种。
1.ViewController中被加入了定时器,而没有及时的invalidate。
#import "AViewController.h"
@interface AViewController ()
{
NSTimer *timer;
}
@end
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
}
-(void)updateTime:(id)sender
{
NSLog(@"%@",sender);
NSLog(@"%@",self);
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[timer invalidate];
}
-(void)dealloc
{
NSLog(@"%@",self);
NSLog(@"AViewController被释放了哦!");
}
在没加上[timer invalidate];之前,不会打印AViewController被释放了哦!信息。也就是在此控制器被pop/dismiss时没有走dealloc方法,没有被释放。加上[timer invalidate];之后控制器正常释放了。
2.ViewController里面存在强引用的属性,比如代理。
新建一个类作为ViewController的属性Aclass。
#import
@protocol AclassProtocolDelegate
-(void)study;
@end
@interface Aclass : NSObject
@property(nonatomic,strong)id
@end
#import "Aclass.h"
@implementation Aclass
-(void)dealloc
{
NSLog(@"Aclass被释放了");
}
@end
#import
#import "Aclass.h"
@interface AViewController : UIViewController
@property (nonatomic,strong) Aclass *aclass;
@end
#import "AViewController.h"
@interface AViewController ()
{
NSTimer *timer;
}
@end
@implementation AViewController
-(void)study
{
NSLog(@"今天学习了吗?");
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
self.aclass = [[Aclass alloc] init];
self.aclass.delegate = self;
NSLog(@"%@",self.aclass);
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateTime:) userInfo:nil repeats:YES];
}
-(void)updateTime:(id)sender
{
NSLog(@"%@",self);
if ([self.aclass.delegate respondsToSelector:@selector(study)]) {
[self.aclass.delegate study];
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[timer invalidate];
}
-(void)dealloc
{
NSLog(@"%@",self);
NSLog(@"AViewController被释放了哦!");
}
@end
结果不会走AViewController的dealloc方法打印出AViewController被释放了哦!因为控制器在被pop/dismiss出栈之后,还被里面的aclass.delegate强引用在,相互循环引用。无法释放。解决办法有两个。A.手动释放在 [timer invalidate];下面将 self.aclass = nil;
need-to-insert-img
B.将代理的属性设置成weak。 @property(nonatomic,weak)id
need-to-insert-img
3.控制器中的block的循环引用。
#import
typedef void (^myBlock)(void);
@interface AViewController : UIViewController
{
myBlock blk;
}
@end
#import "AViewController.h"
@implementation AViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
blk = ^{
NSLog(@"self = %@", self);
};
}
-(void)dealloc
{
NSLog(@"%@",self);
NSLog(@"AViewController被释放了哦!");
}
@end
这样写系统会提示Capturing ’self’ strongly in this block is likely to lead to a retain cycle.
need-to-insert-img
当pop/dismiss控制器的时候不会走dealloc方法,因为存在循环引用,控制器强引用了block,而block又强引用了控制器,不能进行释放。由于self是__strong修饰,在 ARC 下,当编译器自动将代码中的 block 从栈拷贝到堆时,block 会强引用和持有self,而self恰好也强引用和持有了 block,就造成了传说中的循环引用。
解决办法:
id__weak weakself = self;
blk = ^{
NSLog(@"self = %@",weakself);
};
即可看到调用dealloc方法,打印AViewController被释放了哦!。
need-to-insert-img
扩展下,如果上面变成如下。则也会形成循环引用。
#import
typedef void (^myBlock)(void);
@interface AViewController : UIViewController
{
myBlock blk;
id _obj;
}
@end
#import "AViewController.h"
@implementation AViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor redColor];
blk = ^{
NSLog(@"_obj = %@", _obj);
};
}
-(void)dealloc
{
NSLog(@"%@",self);
NSLog(@"AViewController被释放了哦!");
}
@end
虽然没有直接使用 self,却也存在循环引用的问题。因为对于编译器来说,_obj就相当于self->_obj,所以上面的代码就会变成。
blk = ^{
NSLog(@"_obj = %@",self->_obj);
};