ovm
ovm

Reputation: 2532

memory leak when decoding a NSArray with NSKeyedUnarchiver decodeObjectForKey

I'm trying to fix a huge memory leak that is caused somewhere in a method, that gets a NSData object from a CoreData datastore and unarchives that NSData back into a NSArray.

- (CustomObject *) getCustomObject
{
.... get myManagedObject from CoreData Store

    CustomObject *customObject = nil;
    if(myManagedObject)
    {
        customObject = [[[CustomObject alloc] init] autorelease];

        NSData *arrayData = [myManagedObject valueForKey:@"resultArray"];

        /* read and save resultArray */
        NSKeyedUnarchiver *unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:arrayData];

        /* per definition decodeObjectForKey returns an autoreleased object */
        /* Instruments (The Profiler) points out this line for the source */
        /* of the memory leak. */
        NSArray *myArray = [unArchiver decodeObjectForKey:@"rArray"];

        /* myArray is a (nonatomic,retain) property in CustomObject */
        [customObject setMyArray:myArray];

        /* finishDecoding and release unarchiver */
        [unArchiver finishDecoding];
        [unArchiver release];
    }

    return customObject;
}

If i release myArray (what i should not do, since it is a autoreleased object) the memory leak disappears.

Does anyone know how you can solve this issue? The items in the NSArray of the CustomObject are NSDictionary objects.

thanks in advance

edit: - (CustomObject *) getCustomObject is called very often, so the impact of this leak is quite big.

Upvotes: 1

Views: 1564

Answers (1)

Abizern
Abizern

Reputation: 150705

If you're finding this from Instruments - it's showing the place where the leaked object is created, not where it is leaked.

What this is saying is that you're passing myArray to something that is not releasing it properly further down the line. Are you releasing the array properly in you customObjects dealloc?

Upvotes: 2

Related Questions