Instruments detect a memory leak:
(Leaked Object= "__NSCFString")
This is my code:
-(NSArray*)loadAllPages{
NSManagedObjectContext *context = [self managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Page" inManagedObjectContext:context];
[fetchRequest setEntity:entity];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:@"date" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObject:sortDescriptor]];
[sortDescriptor release];
NSError *error = nil;
myArray = [[NSArray alloc ]initWithArray:[[context executeFetchRequest:fetchRequest error:&error]autorelease]];
[fetchRequest release];
if (myArray == nil)
NSLog(@"%@",error);
return myArray;
}
The line where leak is pointed at is initialization of myArray.
If I do not return myArray, this leak doesn’t occur, so this is strange for me.
In this line
the square brackets are slightly wrong.
The
autoreleaseis sent to the result of[context executeFetchRequest:...], not to the allocated array.[context executeFetchRequest:...]returns an autoreleased array. Sendingautoreleaseto that array is an error. AndmyArrayis allocated, but not released in your code.The Xcode static analyzer also reports this as an error:
I assume that you meant
which would result in an autoreleased array
myArray.