2

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

- (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 秒)

4

3 回答 3

5

不要打电话-[NSTimer dealloc]。曾经。

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

于 2010-03-19T21:52:11.647 回答
3

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

于 2010-03-19T21:49:14.097 回答
1

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

于 2010-03-19T22:16:10.527 回答