简体   繁体   中英

Core Data EXC_BAD_ACCESS fail on save?

My Core Data model just won't save. I can insert one kind of object (Graph - see below) to it, and saving will work. I can add multiple copies of that kind of object and saving will work. When I insert a different object, any one, it fails, usually with this:

#2  0x00007fff86c342c8 in -[NSManagedObjectContext(_NSInternalAdditions) _changeIDsForManagedObjects:toIDs:] ()

The other objects are not complicated: just String and Number values. Their values are set at save time. I have reduced the program to just the initialisation of the NSPersistentDocument: the following is the relevant code called in initWithType:error: in my NSPersistentDocument subclass:

...

// Disable undo registration before inserting default objects
[[[self managedObjectContext] undoManager] disableUndoRegistration];

// Insert the root graph
self.root = [NSEntityDescription insertNewObjectForEntityForName:@"Graph" inManagedObjectContext:[self managedObjectContext]];
self.root.createdDate = [NSDate date];
self.root.lastModifiedDate = [NSDate date];
self.root.name = @"Untitled Model";
self.root.colour = [NSColor colorWithDeviceRed:0.6 green:0.6 blue:0.6 alpha:1.0];

// Insert default drawing parameters
// fails when including this line
self.drawing = [NSEntityDescription insertNewObjectForEntityForName:@"Drawing" inManagedObjectContext:[self managedObjectContext]];

[[self managedObjectContext] processPendingChanges];
[[[self managedObjectContext] undoManager] enableUndoRegistration];

...

When run, I get:

2011-07-23 07:46:58.012 Z2[15774:a0f] Managed Object Context: <NSManagedObjectContext: 0x2000c65e0>
2011-07-23 07:46:58.013 Z2[15774:a0f] Persistent Store Coordinator: <NSPersistentStoreCoordinator: 0x200063700>
2011-07-23 07:46:58.013 Z2[15774:a0f] Entity: Graph
...<full Entity list>
2011-07-23 07:46:58.016 Z2[15774:a0f] Entity: Drawing
2011-07-23 07:46:58.018 Z2[15774:a0f] Object: <Graph: 0x20004e220> (entity: Graph; id: 0x20003e400 <x-coredata:///Graph/t527187E0-DC67-4A9E-BA88-67A4CD3445222> ; data: {
colour = "(...not nil..)";
createdDate = "2011-07-22 21:46:54 +0000";
errorString = nil;
firstDate = nil;
lastDate = nil;
lastModifiedDate = "2011-07-22 21:46:54 +0000";
lines =     ();
name = "Untitled Model";
nodes =     ();
profiles =     ();
test = 0;
warningString = nil;
})
2011-07-23 07:46:58.019 Z2[15774:a0f] Object: <Drawing: 0x20004f140> (entity: Drawing; id: 0x200062280 <x-coredata:///Drawing/t527187E0-DC67-4A9E-BA88-67A4CD3445223> ; data: {
centrex = 0;
centrey = 0;
textSize = 12;
typeface = "Helvetica Neue";
zoomFactor = 1;
})
Program received signal:  “EXC_BAD_ACCESS”.

Every Entity has its own class that subclasses NSManagedObject. Every attribute is defined as a dynamic property. Every attribute has a default value, where it's reasonable to have them.

I have seen others suggest over-releasing for this type of problem, but I do not perform retain and release anywhere in the Core Data code. It doesn't matter which Entity I choose to insert after the Graph object; they all fail.

The Core Data work is done by a framework (Graph). The framework is called from a document-based application (Z) that subclasses the framework's document-creating object.

Here's the stack trace from the end of the save panel:

Program received signal:  “EXC_BAD_ACCESS”.
sharedlibrary apply-load-rules all
(gdb) bt
#0  0x00007fff80352480 in CFDictionarySetValue ()
#1  0x00007fff86c0b2ed in _PFCMT_SetValue ()
#2  0x00007fff86c342c8 in -[NSManagedObjectContext(_NSInternalAdditions) _changeIDsForManagedObjects:toIDs:] ()
#3  0x00007fff86c44b2b in -[NSSQLCore commitChanges:] ()
#4  0x00007fff86c3926e in -[NSSQLCore saveChanges:] ()
#5  0x00007fff86bfcc8b in -[NSSQLCore executeRequest:withContext:] ()
#6  0x00007fff86bfc051 in -[NSPersistentStoreCoordinator(_NSInternalMethods) executeRequest:withContext:] ()
#7  0x00007fff86c30123 in -[NSManagedObjectContext save:] ()
#8  0x00007fff8144179d in -[NSPersistentDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] ()
#9  0x0000000100020460 in -[GraphDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] (self=0x2000c4be0, _cmd=0x7fff816eb6ce, absoluteURL=0x20004eec0, typeName=0x20000ea80, saveOperation=1, absoluteOriginalContentsURL=0x0, outError=0x7fff5fbfe308) at /<path to>/GraphDocument.m:380
#10 0x00000001000010bc in -[ZDocument writeToURL:ofType:forSaveOperation:originalContentsURL:error:] (self=0x2000c4be0, _cmd=0x7fff816eb6ce, absoluteURL=0x20004eec0, typeName=0x20000ea80, saveOperation=1, absoluteOriginalContentsURL=0x0, outError=0x7fff5fbfe308) at /<path to>/ZDocument.m:110
#11 0x00007fff81316bd2 in -[NSDocument _writeSafelyToURL:ofType:forSaveOperation:error:] ()
#12 0x00007fff81315fd0 in -[NSDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
#13 0x00007fff814406dc in -[NSPersistentDocument writeSafelyToURL:ofType:forSaveOperation:error:] ()
#14 0x00007fff81311356 in -[NSDocument saveToURL:ofType:forSaveOperation:error:] ()
#15 0x00007fff81314404 in -[NSDocument _saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] ()
#16 0x00007fff81311637 in -[NSDocument saveToURL:ofType:forSaveOperation:delegate:didSaveSelector:contextInfo:] ()
#17 0x00007fff8131377c in -[NSDocument _savePanelWasPresented:withResult:inContext:] ()
#18 0x00007fff8148335e in -[NSSavePanel _didEndSheet:returnCode:contextInfo:] ()
#19 0x00007fff81227d45 in -[NSApplication endSheet:returnCode:] ()

Since it fails on the change of object IDs, I'm going to guess the problem is with a relationship. Most likely the problem is with a required relationship, a delete rule or a validation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM