Every time I call this method my NSMutableData is leaking and I cannot figure out how to plug it. theData’s retain count is upped by one after the decoder is allocated and initialized and I have no idea why. I am stuck with a retain count of 2 at the end of the method and attempting to release it causes an app crash.
- (void)readVenueArchiveFile:(NSString *)inFile key:(NSString *)inKey { NSMutableData *theData; NSKeyedUnarchiver *decoder; theData = [NSData dataWithContentsOfFile:inFile]; decoder = [[NSKeyedUnarchiver alloc] initForReadingWithData:theData]; venueIOList = [[decoder decodeObjectForKey:inKey] mutableCopy]; [decoder finishDecoding]; [decoder release]; }
I would suggest replacing this line:
with:
This makes the memory management of
decodedListclear. It is considered best practice to assign instance variables using an accessor method (except in init methods). In your current implementation, if you ever invokereadVenueArchiveFile:a second time on the same object, you will leak (as you will ifdecodedListalready has a value). Moreover, you can put the copy logic in your accessor method and forget about it rather than having to remember mutableCopy every time you assign a new value (assuming there’s a good reason to make a mutable copy anyway?).