NSTimeInterval内存泄漏
我的 NSTimeIntervall 和 NSDate 出现了奇怪的内存泄漏。这是我的代码:
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 只是一个 NSDate 对象,这应该没问题。仪器告诉我“间隔”泄漏,但我不太明白这一点,如何释放非对象?该函数在我在此处发布的代码片段之后结束,因此根据我的理解,间隔应该会自动释放。
多谢!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它可能告诉您该线路正在发生泄漏。
表达式
[[[Config alloc] getCacheLifetime] integerValue]
是您的问题。首先,您关心创建一个对象(调用
alloc
),但在调用release
或autorelease
之前丢失了对它的引用,所以它是泄漏。另外,您确实应该在分配对象后立即调用
init
方法。即使您的 Config 类没有执行任何特殊操作,也需要调用 NSObject 的 init 方法。如果您用 替换该管线 则
泄漏应该被堵住。
您还泄漏了 maxCacheAge 对象。在 if 语句之前插入
[maxCacheAge autorelease];
应该可以解决这个问题。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 callingrelease
orautorelease
, so it is leaking.Also, you really ought to call an
init
method immediately after allocating the object. Even if yourConfig
class doesn't do anything special,NSObject
'sinit
method will need to be called.If you replace that line with
That leak should be plugged up.
You are also leaking the
maxCacheAge
object. Inserting[maxCacheAge autorelease];
before the if statement should fix that.找到问题了,如果您遇到同样的问题,这就是解决方案:
问题是 maxCacheAge 对象需要释放,因为我拥有它(请参阅下面的链接)。
我得到了它,这要归功于这里很棒的解决方案:iPhone内存管理
Found the problem, in case you come across the same issue, this is the solution:
The problem is that the maxCacheAge object needs to get released, as I own it (see link below).
I got it thanks to the awesome solution here: iPhone Memory Management