Iphone NSTimeInterval内存泄漏

Iphone NSTimeInterval内存泄漏,iphone,memory-leaks,nstimeinterval,Iphone,Memory Leaks,Nstimeinterval,我的NSTimeIntervall和NSDate有一个奇怪的内存泄漏。这是我的密码: NSTimeInterval interval = 60*60*[[[Config alloc] getCacheLifetime] integerValue]; NSDate *maxCacheAge = [[NSDate alloc] initWithTimeIntervalSinceNow:-interval]; if ([date compare:maxCacheAge] == NSOrderedDe

我的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对象,这应该可以。仪器告诉我“间隔”泄漏,但我不太明白这一点,我如何才能释放一个非对象?函数在我在这里发布的代码片段之后结束,因此据我所知,interval应该会自动取消分配


非常感谢

发现问题,如果遇到相同的问题,以下是解决方案:

[[ClubzoneConfig alloc] loadConfigFile];
NSTimeInterval interval = 60*60*[[[ClubzoneConfig alloc] getCacheLifetime] integerValue];
NSDate *maxCacheAge = [[NSDate alloc] initWithTimeIntervalSinceNow:-interval];

if ([date compare:maxCacheAge] == NSOrderedDescending) {
    [maxCacheAge release];
    return YES;
} else {
    [maxCacheAge release];
    return NO;
}
问题是maxCacheAge对象需要释放,因为我拥有它(请参阅下面的链接)


多亏了这里的绝妙解决方案:

找到了问题,如果遇到同样的问题,这就是解决方案:

[[ClubzoneConfig alloc] loadConfigFile];
NSTimeInterval interval = 60*60*[[[ClubzoneConfig alloc] getCacheLifetime] integerValue];
NSDate *maxCacheAge = [[NSDate alloc] initWithTimeIntervalSinceNow:-interval];

if ([date compare:maxCacheAge] == NSOrderedDescending) {
    [maxCacheAge release];
    return YES;
} else {
    [maxCacheAge release];
    return NO;
}
问题是maxCacheAge对象需要释放,因为我拥有它(请参阅下面的链接)


我得到它要感谢这里令人敬畏的解决方案:

它可能告诉您该管线上正在发生泄漏

表达式
[[[Config alloc]getCacheLifetime]integerValue]
是您的问题

首先,您关心创建一个对象(调用
alloc
),但在调用
release
autorelease
之前,您丢失了对它的引用,因此它正在泄漏

另外,在分配对象之后,您确实应该立即调用
init
方法。即使您的
Config
类没有执行任何特殊操作,也需要调用
NSObject
init
方法

如果你把那条线换成

Config *config = [[Config alloc] init];
NSTimeInterval interval = 60*60*[[config getCacheLifetime] integerValue];
[config release];
那个漏洞应该堵住


您还泄漏了
maxCacheAge
对象。插入
[maxCacheAge自动释放]应该可以解决这个问题。

它可能告诉您该行正在发生泄漏

表达式
[[[Config alloc]getCacheLifetime]integerValue]
是您的问题

首先,您关心创建一个对象(调用
alloc
),但在调用
release
autorelease
之前,您丢失了对它的引用,因此它正在泄漏

另外,在分配对象之后,您确实应该立即调用
init
方法。即使您的
Config
类没有执行任何特殊操作,也需要调用
NSObject
init
方法

如果你把那条线换成

Config *config = [[Config alloc] init];
NSTimeInterval interval = 60*60*[[config getCacheLifetime] integerValue];
[config release];
那个漏洞应该堵住


您还泄漏了
maxCacheAge
对象。插入
[maxCacheAge自动释放]应该可以解决这个问题。

嗯,您使用
[ClubzoneConfig alloc]
的方式看起来是错误的。更多信息请参见我的答案。嗯,您使用
[ClubzoneConfig alloc]
的方式看起来是错误的。更多信息请参见我的答案。嘿,谢谢你的解答!现在我也明白了为什么间隔会泄漏。我应用了您的解决方案,它似乎很有效,可能比我的想法要好得多。我还想指出,静态分析器(Xcode>Build>Build and Analysis)可以捕获这两个漏洞,而无需运行仪器。嘿,感谢您提供的解决方案!现在我也明白了为什么间隔会泄漏。我应用了您的解决方案,它似乎很有效,可能比我的想法要好得多。我还想指出,静态分析器(Xcode>Build>Build and Analyze)捕获了这两个漏洞,而无需运行仪器。