スレッド内でタイマーを使用しています。常にタイマーをインスタンス化していますが、機器内でアプリケーションを実行すると、メモリが指数関数的に増加し、リークは発生していません。NSThreadが成長を引き起こしていると確信していますか?。新しいタイマー(シングルトン)を実行する前に現在のタイマーを停止し、stopTimerですべてを解放していますが、メモリがまだ増加している理由がわかりませんか?
- (void)startingTimer{
[view addSubview:timerBackground];
timerThread = [[NSThread alloc] initWithTarget:timer selector:@selector(startTimerThread) object:nil]; //Create a new thread
[timerThread start]; //start the thread
}
//the thread starts by sending this message
- (void) startTimerThread{
NSAutoreleasePool * timerNSPool = [[NSAutoreleasePool alloc] init];
NSRunLoop* runLoop = [NSRunLoop currentRunLoop];
isTimerRunning = YES;
nsTimer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(startTime:) userInfo:nil repeats:YES];
[runLoop run];
[timerNSPool release];
}
- (void)startTime:(NSTimer *)theTimer{
if(timeDuration > 1){
--timeDuration;
//[self updateTextLbl];
[timer performSelectorOnMainThread:@selector(updateTextLbl) withObject:nil waitUntilDone:YES];
}else{
[timer stopTimer];
if(delegate)
[delegate timeIsUp];
}
}
- (void) stopTimer{
isTimerRunning = NO;
[nsTimer invalidate];
[timerThread release];
nsTimer = nil;
timerBackground.alpha = 0.0;
timerBackground.frame = CGRectMake(TIMER2_BG_X - TIMER2_BG_WIDTH, TIMER2_BG_Y, TIMER2_BG_WIDTH, TIMER2_BG_HEIGHT);
}
- (void)updateTextLbl{
timeLabel.text = [NSString stringWithFormat:@"%d",timeDuration];
}