Crashing on saving a managedObjectContext, with 'NSInvalidArgumentException', but only sporadically

frandogger picture frandogger · Aug 20, 2010 · Viewed 8.3k times · Source

I keep getting crashes from a save: command on a managedObjectContext. It doesn't even fulfill the NSLog statement so I don't see the unresolved error statement, so I can't figure out what the problem might be. It doesn't happen every time, but only sporadically.

Here's the code (which basically wants to increment a counter):

 if ([[managedObject valueForKey:@"canSee"]boolValue]){
    int read = [[managedObject valueForKey:@"timesRead"] intValue] +1;
    [managedObject setValue:[NSNumber numberWithInt:read] forKey:@"timesRead"]; 


    NSError *error;
    if (![resultsController.managedObjectContext save:&error]) {  //<-- crashes on this line!
        NSLog(@"Unresolved Core Data Save error %@, %@", error, [error userInfo]);
        exit(-1);
    }

In the console window I get messages like this:

  2010-08-20 08:12:20.594 AppName[23501:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet controllerWillChangeContent:]: unrecognized selector sent to instance 0xe54f560'

or this:

  2010-08-20 08:12:20.594 AppName[23501:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFSet controllerWillChangeContent:]: unrecognized selector sent to instance 0xe54f560'

or even this:

  2010-08-19 23:09:59.337 AppName[761:307] Serious application error.  Exception was caught during Core Data change processing.  This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification.  -[UITableViewLabel controllerWillChangeContent:]: unrecognized selector sent to instance 0x7f0a860 with userInfo (null)
  2010-08-19 23:09:59.356 AppName[761:307] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewLabel controllerWillChangeContent:]: unrecognized selector sent to instance 0x7f0a860'

Then it shows the call stack at first throw, followed by a notice (terminate called after throwing an instance of 'NSException', '[Switching to process 23501]' and 'Program received signal: “SIGABRT”.'

I think the problem has something to do with CoreData but I'm not sure. I've cleaned my build and targets and it doesn't seem to help. I've tried locking/unlocking the ManagedObjectContext and it doesn't help.

Any ideas here on where to start to look for a resolution would be greatly appreciated!

Answer

Marcus S. Zarra picture Marcus S. Zarra · Aug 20, 2010

Looks like you are releasing a UIViewController and not releasing its associated NSFetchedResultsController. The NSFetchedResultsController is trying to notify its delegate (most likely your UIViewController) of the save on exit.