initWithContentsOfFile泄漏问题 [英] Leak problem with initWithContentsOfFile

查看:157
本文介绍了initWithContentsOfFile泄漏问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用运行良好,看起来还不错.但是,在使用Instruments运行它之后,我发现其上存在大量泄漏.似乎有几件事像下面的代码一样没错.代码真的有问题吗?任何一个单词都会对我有帮助.

My app is running good and looks ok. But after I ran it with Instruments, I found tons of leaks on it. There are several things seems not wrong like a code below. Is the code really have problems? Any single word will be helpful to me.

@interface GameData : NSObject
{
    NSDictionary* _data;
    NSDictionary* _localData;
}
@end

@implementation GameData

- (id) init
{
    NSString* dataFilename = [[NSBundle mainBundle]pathForResource:@"GameData" ofType:@"plist"];
    _data = [[NSDictionary alloc]initWithContentsOfFile:dataFilename]; // Leaks 48 bytes

    NSString* localDataFilename = [[NSBundle mainBundle]pathForResource:@"GameData-Local" ofType:@"plist"];
    _localData = [[NSDictionary alloc]initWithContentsOfFile:localDataFilename];

    return self;
}

- (void) dealloc
{
    [_data release];
    [_localData release];

    [super dealloc];
}

@end

推荐答案

某些操作可能导致框架存储从不发布的静态数据结构.例如,-initWithContentsOfFile:的实现可能会在首次使用它时设置一些内部设置,这些设置会一直存在,直到应用终止为止,这可能是出于性能优化的原因.这不是真正的泄漏,但是泄漏检测软件有时会对其进行标记.框架本身也有可能导致内存泄漏的错误,但这很少见,尤其是对于像NSDictionary这样的成熟类而言.

Some operations may cause the frameworks to store static data structures that are never released. For instance, the implementation of -initWithContentsOfFile: may set up some internal settings the first time it's used which are then in place until the app is terminated, possibly for performance optimization reasons. This is not a real leak, but leak detection software will sometimes flag it as such. There's also the possibility that the framework itself has a bug that causes a memory leak but this is rare, especially for a well-established class like NSDictionary.

您没有泄漏代码,据我所知这是正确的.如果正在调用您的-dealloc方法(请确保添加一条日志语句),则您正在履行合同的一部分,任何泄漏都不是您的错.

You're not leaking in your code, it's correct as far as I can tell. If your -dealloc method is being called (add a log statement to be sure) then you are fulfilling your part of the contract and any leak is not your fault.

使用ObjectAlloc仪器可能值得,因为这可以使您更好地了解分配了哪些对象并在其中徘徊.

It's probably worth using the ObjectAlloc instrument as this gives you a much better idea of what objects are allocated and are hanging around.

这篇关于initWithContentsOfFile泄漏问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆