IOS的ARC会致使的内存泄露问题和解决方案

下面列举两种ARC致使内存泄露的状况。

1,循环参照

A有个属性参照B,B有个属性参照A,若是都是strong参照的话,两个对象都没法释放。

这种问题常发生于把delegate声明为strong属性了。

例,

@interface SampleViewController

@property (nonatomic, strong) SampleClass *sampleClass;

@end

@interface SampleClass

@property (nonatomic, strong) SampleViewController *delegate;

@end

 

上例中,解决办法是把SampleClass 的delegate属性的strong改成week便可

 

2,死循环

若是某个ViewController中有无限循环,也会致使即便ViewController对应的view关掉了,ViewController也不能被释放。

这种问题常发生于animation处理。

例,

好比,

CATransition *transition = [CATransition animation];

transition.duration = 0.5;

tansition.repeatCount = HUGE_VALL;

[self.view.layer addAnimation:transition forKey:"myAnimation"];

 

上例中,animation重复次数设成HUGE_VALL,一个很大的数值,基本上等于无限循环了。

解决办法是,在ViewController关掉的时候,中止这个animation。

-(void)viewWillDisappear:(BOOL)animated {

    [self.view.layer removeAllAnimations];

}
相关文章
相关标签/搜索