NSCFTimer 是否存在内存泄漏?
我用仪器追踪到了内存泄漏。我最后总是得到负责图书馆是基金会的信息。当我在代码中追踪到这一点时,我最终到了这里,但我的内存管理没有任何问题:
- (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不要调用
-[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.除非您的
stopAnimationTimer
方法是invalidate
'ing 和release
'ing(然后设置为nil
)您的animationTimer
属性,你正在泄漏内存。Unless your
stopAnimationTimer
method isinvalidate
'ing andrelease
'ing (and then setting tonil
) youranimationTimer
property, you're leaking memory.我发现了:我对我的计时器有强烈的参考。运行循环保留它。所以 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.