内存泄露
普通 普通的就是使用自动释放池和一段使用strong,一端使用weak
block 防止循环引用, 使用__weak
CG 创建的同时,记得使用完成之后释放 CGRealse
通知 dealloc
NSTimer
以下代码的问题:
@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!");}
1)timer都会对它的target进行retain,对于重复性的timer,除非手动关闭,否则对象不会释放,场景:导航控制器关联的控制器无法销毁 2)NSTimer要加到异步线程中,防止线程繁忙导致定时器失准
3)timer必须加入到runloop中才会有效,主线程runloop默认开启,异步线程手动启动
4)注意runloop模式