NSTimer 火灾问题(不是常见的无效问题)

发布于 2024-11-04 21:45:10 字数 1219 浏览 0 评论 0原文

我的代码如下:

- ( void ) restart {
    [ self pause ];
    //init something here...
    [ self proceed ];
}

- ( void ) pause {
    if ( [ _timer isValid ] ) {
        [ _timer invalidate ];
        _timer = nil;
    }
}

- ( void ) proceed {
    _timer = [ NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector ( fireHandler ) userInfo: nil repeats: YES ];
}

大多数情况下它工作正常,但我发现在某些情况下,在我多次调用“重新启动”方法后,有时,fireHandler 在单个 timeInterval(或 timeInterval)内被调用多次变短了,反正突然跑得更快了)。

现在我将发送者添加到 fireHandler 中,并使用 NSLog 检查内存地址,我发现如下内容:

<_NSCFTimer: 0x4e610a0> <_NSCFTimer:0x4e610a0> <_NSCFTimer:0x4e610a0> <_NSCFTimer:0x4e610a0> <_NSCFTimer:0x4e610a0> <_NSCFTimer:0x4e610a0>

并重复...这意味着它始终是同一个计时器。 但有时当我再次调用“重新启动”时,它会指示

<_NSCFTimer: 0x4e3d740> <_NSCFTimer:0x4e610a0> <_NSCFTimer:0x4e3d740> <_NSCFTimer:0x4e610a0> <_NSCFTimer:0x4e3d740> <_NSCFTimer: 0x4e610a0>...repeats

表示有两个定时器,看来第一个还没有按时失效释放。它与第二个同时运行,几秒钟后,在第二个被移除后,即使第一个还活着!

过一段时间它会再次出现。这种情况并不常见,没有规律的话,大约有30%的时间会发生。我对此没有任何想法...如果有人能给我一些想法来解决确切的问题,至少。

my code as below:

- ( void ) restart {
    [ self pause ];
    //init something here...
    [ self proceed ];
}

- ( void ) pause {
    if ( [ _timer isValid ] ) {
        [ _timer invalidate ];
        _timer = nil;
    }
}

- ( void ) proceed {
    _timer = [ NSTimer scheduledTimerWithTimeInterval: 0.1 target: self selector: @selector ( fireHandler ) userInfo: nil repeats: YES ];
}

Mostly it works fine, but I found that in some case, after I invoked the method "restart" more than one time, sometimes, the fireHandler had been invoked more than one time within a single timeInterval(or the timeInterval was become shorter, anyway, it runs faster suddenly).

Now I add the sender to the fireHandler, and use NSLog to check the memory address, I found something as below:

<_NSCFTimer: 0x4e610a0>
<
_NSCFTimer: 0x4e610a0>
<_NSCFTimer: 0x4e610a0>
<
_NSCFTimer: 0x4e610a0>
<_NSCFTimer: 0x4e610a0>
<
_NSCFTimer: 0x4e610a0>

and repeat... It means that it is always the same timer.
But sometimes when I call "restart" again, it indicates

<_NSCFTimer: 0x4e3d740>
<
_NSCFTimer: 0x4e610a0>
<_NSCFTimer: 0x4e3d740>
<
_NSCFTimer: 0x4e610a0>
<_NSCFTimer: 0x4e3d740>
<
_NSCFTimer: 0x4e610a0>...repeats

It means there are two timer, it seems that the first one has not been invalidate and release on time. It runs with the second one at the same time, and some seconds later, after the second one has be removed, even that the first one still alive!

It will appear again after a while. Such situation is not very often, without rules, it happens about 30% of the whole time. I have not any idea about this....Please if anyone can give me some idea to get the exactly problem, at least.

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

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

发布评论

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

评论(1

享受孤独 2024-11-11 21:45:10

[NSTimer ScheduledTimerWithTimeInterval] 每次调用它时都会创建并安排一个新的计时器。因此,当您多次调用 proceed 时,系统每次都会创建并启动一个新计时器,从而覆盖您的 _timer 引用并造成泄漏。

我不能说为什么你会得到描述的行为,但意外地多次调用 proceed 可能是原因。考虑像这样重写它,看看是否有帮助:

- (void)proceed 
{
    [_timer invalidate];
    _timer = [NSTimer schedule...];
}

[NSTimer scheduledTimerWithTimeInterval] creates and schedules a new timer each time you call it. So when you call proceed several times, the system creates and starts a new timer each time, overriding your _timer reference and creating leaks.

I can't say why you get the behavior to describe, but accidentally calling proceed several times could be the cause. Consider rewriting it like this instead and see if that helps:

- (void)proceed 
{
    [_timer invalidate];
    _timer = [NSTimer schedule...];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文