1

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.

ovm
  • 2,452
  • 3
  • 28
  • 51
  • 1
    You need to start tracing in down the path of `[customObject setMyArray:myArray];` Make sure that you properly release myArray in the customObject dealloc method, if it is not there check any where else that may be retaining the array. – Joe Jul 12 '11 at 14:15
  • 1
    a void method cannot return a value! – Felix Jul 12 '11 at 14:16

1 Answers1

2

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?

Abizern
  • 146,289
  • 39
  • 203
  • 257
  • ok sorry, it was a chain of problems, which is solved now. I commented out the deallocation of myArray in CustomObject dealloc, since it crashed there. But the crash had to do with a inbalanced release somehwere else. – ovm Jul 12 '11 at 14:32