Search code examples
iphonememory-managementmemory-leaksinstruments

Is there an NSCFTimer memory leak?


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)


Solution

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