+ (NSArray *) fetchAllContactsInContext:(NSManagedObjectContext *)a_context
{
NSFetchRequest *_request = [[NSFetchRequest alloc] init];
[_request setEntity:[NSEntityDescription entityForName:@"Contact" inManagedObjectContext:a_context]];
NSSortDescriptor *_sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"lastName" ascending:YES];
NSArray *_sortDescriptors = [[NSArray alloc] initWithObjects:_sortDescriptor, nil];
[_request setSortDescriptors:_sortDescriptors];
NSError *_fetchError=nil;
NSArray *_results = [[NSArray alloc] initWithArray:[a_context executeFetchRequest:_request error:&_fetchError]];
[_sortDescriptor release];
[_sortDescriptors release];
[_request release];
if (_fetchError){
NSLog(@"Contact - Error fetching contacts %@", [_fetchError localizedDescription]);
}
[_fetchError release];
return [_results autorelease];
}
I wanna ask, Is this function leaking memory? Actually Instruments is saying that this function is leaking hell lot of memory.
Can you please help me resolving the memory issue?
If you need to see where retains, releases and autoreleases occur for an object use instruments:
Run in instruments, in Allocations set “Record reference counts” on on (you have to stop recording to set the option). Cause the problem code to run, stop recording, search for there ivar of interest, drill down and you will be able to see where all retains, releases and autoreleases occurred.
Here is a simplifies version using ARC for iOS 4.3 and up: