Re: Crashing resetting or releasing an NSManagedObjectContext
Re: Crashing resetting or releasing an NSManagedObjectContext
- Subject: Re: Crashing resetting or releasing an NSManagedObjectContext
- From: Daniel Kennett <email@hidden>
- Date: Thu, 30 Apr 2009 17:51:51 +0100
Thank you to you and Keary for your reply.
This is what the // copy out some data code does:
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[[[pet valueForKey:@"name"] copy] autorelease]
forKey:@"name"];
[dict setValue:[[[pet valueForKey:@"birthday"] copy] autorelease]
forKey:@"birthday"];
// More of the same
I did as you suggested and now pass in my own MOC. Now I get a
different crash that I've seen before and had managed to hide with
another memory management error. This time, I get an EXC_BAD_ACCESS
when the MOC is dealloc'ing itself:
[NSManagedObjectContext(_NSInternalAdditions)
_disposeObjects:count:notifyParent:]
[NSManagedObjectContext(_NSInternalAdditions) _dispose:]
[NSManagedObjectContext dealloc]
Now, in my mind it seems that it's crashing when trying to dispose an
object that's already been released. Here's how I now get and create
my Pet object:
NSError *fetchError = nil;
NSArray *fetchResults = nil;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]
autorelease];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Pet"
inManagedObjectContext:context];
[fetchRequest setEntity:entity];
fetchResults = [context executeFetchRequest:fetchRequest
error:&fetchError];
if ((fetchResults != nil) && ([fetchResults count] == 1) &&
(fetchError == nil))
{
NSManagedObject *pet = [fetchResults objectAtIndex:0];
return pet;
}
I really can't see what I'm doing wrong. I'll go try NSZombieEnabled
and see what I can find.
Thanks,
-- Daniel
_______________________
email@hidden
http://www.kennettnet.co.uk
Please include previous messages in any reply you send.
On 29 Apr 2009, at 16:32, Alexander Spohr wrote:
Daniel,
You are trying to fetch an object and keep it - but you want to
ignore / throw away the NSManagedObjectContext. This will never
work. The NSManagedObjectContext keeps the object. Your Pet can not
exist without its NSManagedObjectContext.
You should let the caller provide a NSManagedObjectContext and fetch
your Pet into that context. Make it the callers responsibility to
get a NSManagedObjectContext not yours.
+ (Pet *)petAtURL:(NSURL *)url inContext:(NSManagedObjectContext
*)aManagedObjectContext
Or copy the pet into something like an NSDictionary and return that.
atze
Am 29.04.2009 um 10:59 schrieb Daniel Kennett:
Hi list,
I'm hoping you guys can help me. I'm loading up a Core Data store,
copying some data out and attempting to clear it all up. I use this
code for my Quicklook plugin, and in parts of my app for previewing
documents in a more advanced manner than Quicklook provides.
This is how I set up my ManagedObjectContext:
+(Pet *)petAtURL:(NSURL *)url {
NSManagedObjectModel *managedObjectModel =
[KNClarusQuickDocumentParser managedObjectModel];
NSPersistentStoreCoordinator *coordinator =
[[[NSPersistentStoreCoordinator alloc]
initWithManagedObjectModel:managedObjectModel] autorelease];
[coordinator addPersistentStoreWithType:NSSQLiteStoreType
configuration:nil
URL:url
options:nil
error:&error];
NSManagedObjectContext *moc = [[NSManagedObjectContext alloc] init];
// ^ Not Autoreleasing here. It's the responsibility of the
caller to release the MOC. Autoreleasing causes crashes.
[moc setPersistentStoreCoordinator:coordinator];
[[moc undoManager] disableUndoRegistration];
NSError *fetchError = nil;
NSArray *fetchResults;
NSFetchRequest *fetchRequest = [[[NSFetchRequest alloc] init]
autorelease];
NSEntityDescription *entity = [NSEntityDescription
entityForName:@"Pet"
inManagedObjectContext:moc];
[fetchRequest setEntity:entity];
fetchResults = [moc executeFetchRequest:fetchRequest
error:&fetchError];
if ((fetchResults != nil) && ([fetchResults count] == 1) &&
(fetchError == nil))
{
NSManagedObject *pet = [[fetchResults objectAtIndex:0] retain];
return [pet autorelease];
}
return nil;
}
And this is how I get the data out and release it:
Pet *pet = [KNClarusQuickDocumentParser petAtURL:url];
// Copy out some data.
NSManagedObjectContext *context = [pet managedObjectContext];
if (context) {
[context reset]; // This call results in EXC_BAD_ACCESS
[context setPersistentStoreCoordinator:nil];
[context release];
}
return [dict autorelease];
-------- End code --------
Different combinations of trying to do this right result in crashes
at different points. Leaving out [context reset] and just releasing
it obviously gives EXC_BAD_ACCESS again. Autoreleasing the MOC in
+petAtURL: causes crashes when the autorelease pool pops. The only
way I can get it to not crash is to -init the MOC and never release
or autorelease it, but that's causing memory leaks!
Is there a good example anywhere of how to set up and tear down a
Core Data document correctly?
Thanks,
-- Daniel
_______________________
email@hidden
http://www.kennettnet.co.uk
Please include previous messages in any reply you send.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden