NSThread内存中的NSTimer会增长吗?

时间:2010-01-23 00:51:56

标签: cocoa nstimer nsthread

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

1 个答案:

答案 0 :(得分:0)

timerThread已正确发布,但您从未在nsTimer中发布stopTimer。我假设您将其存储在指定了retain的媒体资源中,然后您需要release它。