NSCFTimer 是否存在内存泄漏?

发布于 2024-08-26 01:43:43 字数 584 浏览 8 评论 0原文

我用仪器追踪到了内存泄漏。我最后总是得到负责图书馆是基金会的信息。当我在代码中追踪到这一点时,我最终到了这里,但我的内存管理没有任何问题:

- (void)setupTimer {
    // stop timer if still there
    [self stopAnimationTimer];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(step:) userInfo:nil repeats:YES];

    self.animationTimer = timer; // retain property, -release in -dealloc method
}

属性animationTimer 保留了计时器。在 -dealloc 中我 -release 它。

现在看起来像是一个框架错误?我检查了 iPhone OS 3.0 和 3.1,每次我像这样使用 NSTimer 时都会出现这个问题。知道还有什么问题吗?

(我的内存泄漏扫描间隔是 0.1 秒。但 5 秒也是如此)

I tracked down a memory leak with instruments. I always end up with the information that the responsible library is Foundation. When I track that down in my code, I end up here, but there's nothing wrong with my memory management:

- (void)setupTimer {
    // stop timer if still there
    [self stopAnimationTimer];

    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:0.2 target:self selector:@selector(step:) userInfo:nil repeats:YES];

    self.animationTimer = timer; // retain property, -release in -dealloc method
}

the property animationTimer is retaining the timer. In -dealloc I -release it.

Now that looks like a framework bug? I checked with iPhone OS 3.0 and 3.1, both have that problem every time I use NSTimer like this. Any idea what else could be the problem?

(my memory leak scan interval was 0.1 seconds. but same thing with 5 seconds)

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

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

发布评论

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

评论(3

欢烬 2024-09-02 01:43:43

不要调用 -[NSTimer dealloc]。曾经。

在这种情况下,-scheduledTimerWithTimeInterval:target:selector:userInfo:repeats:-invalidate 平衡。您不需要在计时器对象上调用 -dealloc-release

Do not call -[NSTimer dealloc]. Ever.

In this case, -scheduledTimerWithTimeInterval:target:selector:userInfo:repeats: is balanced by -invalidate. You do not need to call -dealloc or -release on the timer object.

我不在是我 2024-09-02 01:43:43

除非您的 stopAnimationTimer 方法是 invalidate'ing 和 release'ing(然后设置为 nil)您的 animationTimer 属性,你正在泄漏内存。

Unless your stopAnimationTimer method is invalidate'ing and release'ing (and then setting to nil) your animationTimer property, you're leaking memory.

Smile简单爱 2024-09-02 01:43:43

我发现了:我对我的计时器有强烈的参考。运行循环保留它。所以 RC 是 2。但是因为计时器也持有对目标的强引用(在我的例子中保留了计时器),所以我遇到了死锁情况。 -dealloc 从未被调用,因此我的计时器从未被释放。 WTF。

I found it: I had a strong reference to my timer. The run loop retains it. So RC was 2. But because the Timer also holds a strong reference to the target (which in my case retained the timer), I had a deadlock situation. -dealloc was never ever called, and therefore my timer was never ever freed. WTF.

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