NSTimer внутри NSThread Память растет? - PullRequest
0 голосов
/ 23 января 2010

Я использую таймер в потоке, я все время его запускаю, однако, когда я запускаю приложение в инструменте, я вижу, что память растет в геометрической прогрессии, у меня нет никаких утечек. Я уверен, что 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 голосов
/ 19 октября 2010

timerThread выпущен правильно, однако вы никогда не отпустите nsTimer в stopTimer. Я предполагаю, что вы храните его в свойстве с указанным retain, а затем вам потребуется release.

...