views:

34

answers:

1

Can anyone shed some light as to why this use of mutableCopy is leaking memory?

- (id)objectInListAtIndex:(unsigned)theIndex {
       NSSortDescriptor *descriptor = [[[NSSortDescriptor alloc] initWithKey:@"noteNumber"  ascending:YES] autorelease];
       [list sortUsingDescriptors:[NSArray arrayWithObjects:descriptor,nil]];
       NSMutableArray *theArray = [list mutableCopy];
       NSDictionary *theDict = [theArray objectAtIndex:theIndex];
       return theDict;
 }
+5  A: 

Because mutableCopy returns a retained object, and you never release theArray.

Copy methods always return a retained object which the caller is responsible for releasing. This is also detailed in the API docs and the memory management guide.

mipadi