NSThread 内的 NSTimer 内存增长?

发布于 2024-08-18 16:32:09 字数 1502 浏览 3 评论 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];
}

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(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];
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初见 2024-08-25 16:32:09

timerThread 已正确释放,但您从未在 stopTimer 中释放 nsTimer。我假设您将其存储在指定了 retain 的属性中,然后您需要release 它。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文