NSFetchedResultsController initWithFetchRequest throws EXE-BAD-ACCESS exception

1.9k views Asked by At

I have an NSFetchedResultsController inside a subclass of UITableViewController inside a UINavigationController. When I run the app, everything works perfectly the first three times I access the view (going to it, then clicking 'Back', then going to it again), but on the fourth (always) it crashes with the following:

-[NSEntityDescription subentitiesByName]: message sent to deallocated instance 0x8b09c80

Any help would be much appreciated.


Here is my getter for the results controller:

- (NSFetchedResultsController*)eventsResultsController {

  if (eventsResultsController_ == nil) {

    NSFetchRequest *aFetchRequest = [[PADataContext sharedInstance] makeGetAllFetchRequestForEntity:@"PAEvent" sortedBy:@"when" ascending:NO];

    // NOTE: crashes on this next line
    NSFetchedResultsController *aFetchedResultsContorller = [[NSFetchedResultsController alloc] initWithFetchRequest:aFetchRequest managedObjectContext:[[PADataContext sharedInstance] managedObjectContext] sectionNameKeyPath:@"whenMonth" cacheName:@"AllEvent"];

    self.eventsResultsController = aFetchedResultsContorller;

    [aFetchedResultsContorller release];

  }

  return eventsResultsController_;

}

This is the code I use to create my NSFetchRequest:

- (NSFetchRequest*)makeGetAllFetchRequestForEntity:(NSString*)entityName sortedBy:(NSString*)sortString ascending:(BOOL)ascending {

  NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init] autorelease];
  NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];
  [fetchRequest setEntity:entity];

  if (sortString != nil) {

    // add sorting information
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:sortString ascending:ascending];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];

    [fetchRequest setSortDescriptors:sortDescriptors];

    [sortDescriptors release];
    [sortDescriptor release];

  }

  [entity release];
  return fetchRequest;

}

The context object is created once and held throughout the lifetime of the app in a singleton.

I have checked the ensure the eventsResultsController_ gets released when the view controller gets dealloced.

In the stack, I'm told it crashed in the initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName method:

#0  0x00f04057 in ___forwarding___
#1  0x00f03f22 in __forwarding_prep_0___
#2  0x00da1b4d in -[NSFetchedResultsController initWithFetchRequest:managedObjectContext:sectionNameKeyPath:cacheName:]
#3  0x00008c94 in -[PAHistoryListTableView eventsResultsController] at PAHistoryListTableView.m:125
#4  0x00008a5b in -[PAHistoryListTableView loadData] at PAHistoryListTableView.m:52
#5  0x00008a2a in -[PAHistoryListTableView viewDidLoad] at PAHistoryListTableView.m:43
...
1

There are 1 answers

4
Rob Napier On BEST ANSWER

You created your entity with a non-retaining call:

NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:[self managedObjectContext]];

And then you released it:

[entity release];

Take out the release. This is trivial to find when you focus on the fact that it was the NSEntityDescription that was over-released. You only use that in one place, so it's easy to find the mistake.