Note that there are some explanatory texts on larger screens.

plurals
  1. POCore Data: EXC_BAD_ACCESS accessing relationship
    text
    copied!<p>I have a crash in a simple Core Data project.</p> <p>I created a new, empty iPhone project with Core Data. Two entities are added to the data model with a to-one, inverse relationship between them. Both have auto-generated NSManagedObject subclasses. The entity Name and Class are set on both.</p> <pre><code>DBMove attribute: moveValue:Integer16 relationship: newPosition -&gt; DBPosition (inverse: move) DBPosition attribute: positionValue:Integer16 relationship: move -&gt; DBMove (inverse: newPosition) </code></pre> <p>There is a custom view controller that creates a DBMove and DBPosition. It then sets the inverse relationship between them and saves it. The managed object context is retrieved from the app delegate; I think this is safe because [NSThread isMultiThreaded] returns false.</p> <pre><code>- (void)newEntries { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = appDelegate.managedObjectContext; DBPosition *newPos = [NSEntityDescription insertNewObjectForEntityForName:@"DBPosition" inManagedObjectContext:context]; newPos.positionValue = [NSNumber numberWithInt:5]; DBMove *newMove = [NSEntityDescription insertNewObjectForEntityForName:@"DBMove" inManagedObjectContext:context]; newMove.moveValue = [NSNumber numberWithInt:2]; newPos.move = newMove; NSError *error; if (![context save:&amp;error]) { NSLog(@"Save error: %@", [error localizedDescription]); } } </code></pre> <p>The view controller then fetches all the DBMoves. Their Integer16 attributes output as expected. <strong>[Edit]</strong> Accessing the relationship causes EXC_BAD_ACCESS in main::@autoreleasepool to occur sometime after looping through <strong>all</strong> the fetch results. Before the crash, all entries of DBMove and DBPosition in the database from previous program runs all print correctly, followed by NSLogs from my ViewController init; the crash appears delayed. It occurs about every 4 out of 5 program runs. <strong>[End Edit]</strong></p> <pre><code>- (void)printMoves { AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate]; NSManagedObjectContext *context = appDelegate.managedObjectContext; NSError *error; NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; NSEntityDescription *entity = [NSEntityDescription entityForName:@"DBMove" inManagedObjectContext:context]; [fetchRequest setEntity:entity]; NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&amp;error]; for (DBMove *move in fetchedObjects) { DBPosition *newPos = move.newPosition; // &lt;--- all's well without this reference! NSLog(@"Move : %d", [move.moveValue intValue]); NSLog(@"Position : %d", [newPos.positionValue intValue]); } } </code></pre> <p>NSZombieEnabled=YES causes the program to run without fail and print the expected values. My context appears to be the same throughout the whole execution.</p> <p>Here is the stack trace:</p> <pre><code>#0 0x018300b2 in objc_msgSend () #1 0x00256ffc in -[_CDSnapshot dealloc] () #2 0x0025573d in -[_CDSnapshot release] () #3 0x00260384 in -[NSManagedObject(_NSInternalMethods) _clearRawPropertiesWithHint:] () #4 0x0026018b in -[NSFaultHandler turnObject:intoFaultWithContext:] () #5 0x002607ba in -[NSManagedObject dealloc] () #6 0x00257b88 in -[_PFManagedObjectReferenceQueue _processReferenceQueue:] () #7 0x00232fe1 in _performRunLoopAction () #8 0x01a654ce in __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ () #9 0x01a6541f in __CFRunLoopDoObservers () #10 0x01a43344 in __CFRunLoopRun () #11 0x01a42ac3 in CFRunLoopRunSpecific () #12 0x01a428db in CFRunLoopRunInMode () #13 0x038fe9e2 in GSEventRunModal () #14 0x038fe809 in GSEventRun () #15 0x0058ed3b in UIApplicationMain () #16 0x0000396d in main at /Users/brian/devel/test2-CoreData/test2-CoreData/main.m:16 </code></pre> <p>All the Core Data setup is from the standard inclusion when I checked "Core Data" on the empty new project.</p> <p>Presumably I'm overreleasing a managed object, but where?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload