Leak problem with initWithContentsOfFile
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) ini开发者_C百科t
{
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
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.
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.
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.
精彩评论