views:

852

answers:

2

I am getting a (what seems to me as a strange) crash with Core Data. From what I can gather it is happening when Core Data does a save and subsequent managedObjectContextDidSave methods are fired.

I am at a loss and really hoping someone can help me out or guide me in the right direction.

The crash report is as follows:

Exception Type:  EXC_BAD_ACCESS (SIGBUS)
Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000b
Crashed Thread:  0

Thread 0 Crashed:
0   libobjc.A.dylib                 0x000026f4 objc_msgSend + 16
1   Foundation                      0x000437a4 NSClassFromObject + 8
2   Foundation                      0x0000ba54 _NSIMPForObjectAndSelector + 4
3   Foundation                      0x00095eae -[NSSortDescriptor compareObject:toObject:] + 110
4   CoreData                        0x000b0a6e +[NSFetchedResultsController(PrivateMethods) _insertIndexForObject:inArray:lowIdx:highIdx:sortDescriptors:] + 174
5   CoreData                        0x000b1496 -[NSFetchedResultsController(PrivateMethods) _postprocessInsertedObjects:] + 342
6   CoreData                        0x000b32d6 -[NSFetchedResultsController(PrivateMethods) _postprocessUpdatedObjects:] + 430
7   CoreData                        0x000b2a5e -[NSFetchedResultsController(PrivateMethods) _managedObjectContextDidChange:] + 498
8   Foundation                      0x0004bbf6 _nsnote_callback + 162
9   CoreFoundation                  0x00050af2 _CFXNotificationPostNotification + 298
10  Foundation                      0x000497f4 -[NSNotificationCenter postNotificationName:object:userInfo:] + 64
11  CoreData                        0x0002e42e -[NSManagedObjectContext(_NSInternalNotificationHandling) _postObjectsDidChangeNotificationWithUserInfo:] + 66
12  CoreData                        0x0007fd26 -[NSManagedObjectContext(_NSInternalChangeProcessing) _createAndPostChangeNotification:withDeletions:withUpdates:withRefreshes:] + 134
13  CoreData                        0x0001670a -[NSManagedObjectContext(_NSInternalChangeProcessing) _postRefreshedObjectsNotificationAndClearList] + 70
14  CoreData                        0x000164ac -[NSManagedObjectContext(_NSInternalChangeProcessing) _processRecentChanges:] + 1656
15  CoreData                        0x0004b5fa -[NSManagedObjectContext processPendingChanges] + 10
16  CoreData                        0x0003e2a4 _performRunLoopAction + 120
17  CoreFoundation                  0x0000fb50 __CFRunLoopDoObservers + 420
18  CoreFoundation                  0x00056a32 CFRunLoopRunSpecific + 1734
19  CoreFoundation                  0x00056356 CFRunLoopRunInMode + 42
20  GraphicsServices                0x00003cb8 GSEventRunModal + 108
21  GraphicsServices                0x00003d64 GSEventRun + 56
22  UIKit                           0x00002768 -[UIApplication _run] + 384
23  UIKit                           0x0000146c UIApplicationMain + 688
24  MyApp                           0x000022d2 main (main.m:14)
25  MyApp                           0x00002248 start + 44
+1  A: 

It looks like one of the items in one of your database arrays is getting released one too many times (and therefore gets deallocated). It's crashing trying to sort your array, making a comparison with a deallocated object.

Try running your program with NSZombieEnabled -- it prevents the deallocation so you can see which object is getting messages sent to it with a retain count of 0.

Matt B.
Matt thanks for the thoughts. Your explanation of what is happening makes sense (theoretically). I have tried running with NSZombieEnabled many times but with no luck...
kdbdallas
Aha! The bad access is at `0x0b`. I bet you're storing a number (the value 11) as a primitive `int` instead of an `NSNumber`. That would also explain the error on the comparison. Go back through your source and ensure that you're giving all your Core Data entities NSNumbers, not ints.
Matt B.
Matt, wouldn't using a int cause a conversion from int to pointer warning? My entire code is warning free and clean to Clang Static Analyzer. I will still double check, but thought I would ask. Thanks for you help so far!
kdbdallas
If you're using KVC to set the number (as you probably would with Core Data), there's no compile-time checking.
Matt B.
Hey Matt. I did some searching and didn't find anywhere with a int/NSInteger instead of a NSNumber, so I thought I would give running with zombies again, and this time I got a hit and with the EXACT same stack trace. The zombie reported: *** -[CFDate class]: message sent to deallocated instance 0x9d87480.So if this is a date stored in Core Data why would it be getting over deallocated and causing a crash?
kdbdallas
I ended up thinking I would take a break from this issue for a bit and just take some time killing any leaks. Well in the process of fixes a couple of leaks this stopped happening! So it seems that it was the miss-memory-management of some objects that were pointers to data managed by core data. Thank you for your help!
kdbdallas
Glad to hear! I try to regularly run my program through the Leaks instrument. It really does tend to help save you headaches like this, and it makes chasing down the leaks easier (since you only need to check new code).
Matt B.
A: 

If you are using a NSFetchedResultsController then there may be an error in that code and it is not easy to see that. I would suggest putting in some log statements into your NSFetchedResultsController delegate methods and watch which one is getting fired.

Since the NSFRC is fired as part of the save routine of the NSManagedObjectContext, an error in its delegate can easily be misinterpreted as an error in the save itself.

Update 1

If its on a date then perhaps when you set that date somewhere you are overreleasing it yourself? Perhaps calling a -release on a [NSDate date]. That type of error would be hidden until Core Data tried to access the date object.

Marcus S. Zarra
Marcus, thank you for your insights. I checked and I am not using NSFetchedResultsControllerDelegate anywhere. All but 1 of my NSFetchedResultsControllers doesn't set the delegate, and the one that does, does NOT implement any of the delegate protocols methods.When I run with Zombies I get: * -[CFDate class]: message sent to deallocated instance 0x9d87480.So if this is a date stored in Core Data why would it be getting over deallocated and causing a crash?
kdbdallas