Iphone – NSKeyedUnarchiver causing memory leak

cocoa-touchiphone

I have a memory leak problem, I'm saving an array into a file using:

[NSKeyedArchiver archiveRootObject:myArray toFile:MyFile]; 

the objects included into the array have the following methods:

- (id)initWithCoder:(NSCoder *)coder
{

[super init];

parameter1 = [[coder decodeObject] retain];
parameter2 = [[coder decodeObject] retain];
parameter3 = [[coder decodeObject] retain];

return self;
 }

- (void)encodeWithCoder:(NSCoder *)coder
{
[coder encodeObject:parameter1];
[coder encodeObject:parameter2];
[coder encodeObject:parameter3];
}

To unarchive the objects I'm using:

myUnarchivedArray = [NSKeyedUnarchiver unarchiveObjectWithFile:myFile];

The application suddenly crashes because the available memory is not enough to continue.

I'm unable to deallocate myUnarchivedArray and the Intruments tool is telling me that the unarchiver is causing the memory leak.

I haven't search too much, I just found the cause of the memory leak, but I was hopping to find someone that has been passed the same problem and has a tip to solve it.:)

Thank you!!
Anna

Best Answer

You're retaining your objects in initWithCoder although the documentation states:

NSKeyedUnarchiver’s implementation, however, returns an autoreleased object, so its life is the same as the current autorelease pool instead of the keyed unarchiver.

Related Topic