0

我在线程中使用计时器,我一直在实例化计时器但是当我在仪器中运行应用程序时,我看到内存呈指数增长,我没有任何泄漏。我确定 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];
}
4

1 回答 1

0

timerThread已正确释放,但您永远不会nsTimerstopTimer. 我假设您将其存储在retain指定的属性中,然后您将需要release它。

于 2010-10-19T08:14:50.450 回答