tags:

views:

357

answers:

2

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
A: 

You can release dataFilename and localDataFilename once you have initialized _data and _localData.

Hetal Vora
No, you can't. These are autoreleased objects and releasing them will cause a crash.
Rob Keniger
+2  A: 

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.

Rob Keniger