I am trying to centralize Core Data calls for each Core Data entity into a Helper Class. Each Helper class contains the entity’s fetch, update, and insert methods. For one entity helper class, I am getting a memory leak when I profile the application at this line:
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
ARC is turned on and the leak appears after the view has been unloaded.
Here are the associated ViewController and Helper Class code:
ViewController.m:
@synthesize location // and other properties...;
- (void)viewDidLoad
{
[self loadLocation];
[super viewDidLoad];
}
- (void)viewDidUnload
{
// Set properties to nil here
[super viewDidUnload];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)loadLocation
{
if (self.locationID.intValue > 0)
{
LocationCoreDataHelper *helper = [[LocationCoreDataHelper alloc] init];
self.location = [helper selectLocationWithPredicate:[NSString stringWithFormat:@"id = %d", self.locationID.intValue]];
if(self.location)
{
// Create a new coordinate of the user's location
CLLocationCoordinate2D coord;
coord.latitude = [self.location.latitude doubleValue];
coord.longitude =[self.location.longitude doubleValue];
// Annotate the point with user's information
MKPointAnnotation *point = [[MKPointAnnotation alloc] init];
point.coordinate = coord;
point.title = self.location.title;
point.subtitle = self.location.subtitle;
// Add the annotated point
[mkMap addAnnotation:point];
// Set the viewable region of the map
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(point.coordinate, 5000, 5000);
[mkMap setRegion:region animated:YES];
}
helper = nil;
}
}
The location property is defined as the entity’s managed object class.
LocationCoreDataHelper.m:
@implementation LocationCoreDataHelper
- (id)init
{
if(self = [super init])
{
// Setup the core data manager if needed
if(managedObjectContext == Nil)
{
managedObjectContext = [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
}
return self;
}
- (Location *)selectLocationWithPredicate:(NSString *)predicateString
{
NSError *error = nil;
// Build the entity and request
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Location" inManagedObjectContext:managedObjectContext];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
if(predicateString)
{
// Set the search criteria
NSPredicate *predicate = [NSPredicate predicateWithFormat:predicateString];
[request setPredicate:predicate];
}
// Perform the search
// LEAK HERE
NSArray *results = [managedObjectContext executeFetchRequest:request error:&error];
if(results.count >0)
{
return (Location *)[results lastObject];
}
else
{
return nil;
}
}
// Other methods here
@end
I can’t figure out why the memory leak is occurring there. Any ideas?
UPDATE #1:
If I replace this:
point.coordinate = coord;
point.title = self.location.title;
point.subtitle = self.location.subtitle;
with this:
point.coordinate = coord;
point.title = @"Title";
point.subtitle = @"Subtitle";
NSLog(@"%@", self.location.title);
I do not get the memory leak. Why is that?
Thanks to Inafziger for pointing me in the right direction. I ended up having to create a custom MKAnnotation class like so:
and I updated my View Controller like so:
By implementing the custom annotation class in this manner, it solved the issue of the memory leak. I’m still not a 100% sure why Instruments pointed the leak to the Core Data call. Perhaps because that is where the NSManagedObject originated and that wasn’t what was getting released properly?