delegate設(shè)為strong造成的內(nèi)存泄露(兩個(gè)對象相互強(qiáng)引用)
NSTimer 造成的內(nèi)存泄露(兩個(gè)對象相互強(qiáng)引用)
self 持有 timer,timer 在初始化時(shí)持有 self昨登,造成循環(huán)引用
// * interface@interface SomeViewController : UIViewController
@property (nonatomic, strong) NSTimer *timer;
@end
//* implementation@implementation SomeViewController
- (void)someMethod
{
timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:@selector(handleTimer:)
userInfo:nil
repeats:YES];
}
@end
解決的方法就是使用 invalidate 方法銷毀掉 timer
- 死循環(huán)造成的內(nèi)存泄露
CATransition *transition = [CATransition animation];
transition.duration = 0.5;
tansition.repeatCount = HUGE_VALL;
[self.view.layer addAnimation:transition forKey:"myAnimation"];
上例中竭鞍,animation重復(fù)次數(shù)設(shè)成HUGE_VALL聋丝,一個(gè)很大的數(shù)值蒲犬,基本上等于無限循環(huán)了陨收。
解決辦法是肛搬,在ViewController關(guān)掉的時(shí)候没佑,停止這個(gè)animation。
-(void)viewWillDisappear:(BOOL)animated {
[self.view.layer removeAllAnimations];
}
- 非oc方法開辟的堆內(nèi)存造成的泄露
使用malloc new 等非oc方法開辟的堆內(nèi)存温赔,需要使用對應(yīng)的回收方法回收內(nèi)存蛤奢,arc不會自動回收這些內(nèi)存。