Я много читал о NSTimers, но я, должно быть, делаю с ними что-то очень неправильное, потому что это практически все утечки, которые обнаруживаются в Leaks Instrument. В столбце «Ответственный кадр» указано -[NSCFTimer или +[NSTimer(NSTimer).
Итак, вот как у меня настроен NSTimer в главном меню. Я сократил его, чтобы просто показать, как настраивается таймер.
.h -
@interface MainMenu : UIView {
NSTimer *timer_porthole;
}
@end
@interface MainMenu ()
-(void) onTimer_porthole:(NSTimer*)timer;
@end
.m -
(в initWithFrame)
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
timer_porthole = [[NSTimer scheduledTimerWithTimeInterval:.05
target:self
selector:@selector(onTimer_porthole:)
userInfo:nil
repeats:YES] retain];
}
return self;
}
При выходе из представления он убивает таймеры:
-(void) kill_timers{
[timer_porthole invalidate];
timer_porthole=nil;
}
И, конечно же, делолок:
- (void)dealloc {
[timer_porthole invalidate];
[timer_porthole release];
timer_porthole = nil;
[super dealloc];
}