内存泄露的点滴

内存泄露异步

普通 普通的就是使用自动释放池和一段使用strong,一端使用weakoop

block 防止循环引用, 使用__weakspa

CG 建立的同时,记得使用完成以后释放 CGRealse.net

通知  dealloc线程

NSTimer  code

如下代码的问题:对象

@interface SvCheatYourself () {
    NSTimer *_timer;
}@end@implementation SvCheatYourself

- (id)init {
    self = [super init];    if (self) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(testTimer:) userInfo:nil repeats:YES];
    }    return self;
}

- (void)dealloc {
    [_timer invalidate];
}

- (void)testTimer:(NSTimer*)timer{
    NSLog(@"haha!");
}

@end 内存

1)timer都会对它的target进行retain,对于重复性的timer,除非手动关闭,不然对象不会释放,场景:导航控制器关联的控制器没法销毁
2)NSTimer要加到异步线程中,防止线程繁忙致使定时器失准

3)timer必须加入到runloop中才会有效,主线程runloop默认开启,异步线程手动启动get

4)注意runloop模式it

相关文章
相关标签/搜索