首页 > 代码库 > 使用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改为assing即可。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]; }内存泄露的情况当然不止以上两种。 即使用了ARC,我们也要深刻理解iOS的内存管理机制,这样才能有效避免内存泄露。