开发者

NSTimer within NSThread Memory grows?

开发者 https://www.devze.com 2022-12-18 01:27 出处:网络
I am using a timer within a thread , i am instantiating the timer all the time however when i run the application within instrument i see the memory grows exponentially, i am not having any leaks. I a

I am using a timer within a thread , i am instantiating the timer all the time however when i run the application within instrument i see the memory grows exponentially, i am not having any leaks. I am sure that NSThread is causing the growth ? . I am stopping the current timer before running a new one (singleton) and i am releasing everything in my stopTimer, but not sure why the memory still grow?

- (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(deleg开发者_开发知识库ate)
            [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];
}


timerThread is released correctly however you never release nsTimer in stopTimer. I assume you store it in a property with retain specified and then you will need to release it.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号