NSTimeInterval内存泄漏

发布于 2024-08-18 20:04:20 字数 458 浏览 1 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

第几種人 2024-08-25 20:04:20

它可能告诉您该线路正在发生泄漏。

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

首先,您关心创建一个对象(调用 alloc),但在调用 releaseautorelease 之前丢失了对它的引用,所以它是泄漏。

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

如果您用 替换该管线 则

Config *config = [[Config alloc] init];
NSTimeInterval interval = 60*60*[[config getCacheLifetime] integerValue];
[config release];

泄漏应该被堵住。

您还泄漏了 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 calling release or autorelease, so it is leaking.

Also, you really ought to call an init method immediately after allocating the object. Even if your Config class doesn't do anything special, NSObject's init method will need to be called.

If you replace that line with

Config *config = [[Config alloc] init];
NSTimeInterval interval = 60*60*[[config getCacheLifetime] integerValue];
[config release];

That leak should be plugged up.

You are also leaking the maxCacheAge object. Inserting [maxCacheAge autorelease]; before the if statement should fix that.

冬天旳寂寞 2024-08-25 20:04:20

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

[[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 对象需要释放,因为我拥有它(请参阅下面的链接)。

我得到了它,这要归功于这里很棒的解决方案:iPhone内存管理

Found the problem, in case you come across the same issue, this is the solution:

[[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;
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文