I have a weird memory leak with NSTimeIntervall and NSDate. Here is my code:
NSTimeInterval interval = 60*60*[[[Config alloc] getCacheLifetime] integerValue];
NSDate *maxCacheAge = [[NSDate alloc] initWithTimeIntervalSinceNow:-interval];
if ([date compare:maxCacheAge] == NSOrderedDescending) {
return YES;
} else {
return NO;
}
date is just an NSDate object, this should be fine. Instruments tells me that “interval” leaks, yet I do not quite understand this, how can I release a non-object? The function ends after the code snippet I posted here, so from my understanding interval should get automatically deallocated then.
Thanks a lot!
It is probably telling you that a leak is happening on that line.
The expression
[[[Config alloc] getCacheLifetime] integerValue]is your problem.First of all, you care creating an object (calling
alloc) but you lose the reference to it before callingreleaseorautorelease, so it is leaking.Also, you really ought to call an
initmethod immediately after allocating the object. Even if yourConfigclass doesn’t do anything special,NSObject‘sinitmethod will need to be called.If you replace that line with
That leak should be plugged up.
You are also leaking the
maxCacheAgeobject. Inserting[maxCacheAge autorelease];before the if statement should fix that.