Re: Crashing resetting or releasing an NSManagedObjectContext
Re: Crashing resetting or releasing an NSManagedObjectContext
- Subject: Re: Crashing resetting or releasing an NSManagedObjectContext
- From: Keary Suska <email@hidden>
- Date: Mon, 4 May 2009 16:25:39 -0600
On May 4, 2009, at 11:59 AM, Alexander Spohr wrote:
You are still having the same error.
Although the advice is sound, the premise is false.
All your objects in the relationships are fetched. Therefore they
are owned by the NSManagedObjectContext. You kill the
NSManagedObjectContext by releasing it. After that point you are not
allowed to touch any of its fetched objects. But you hand them out
inside the NSDictionary.
The code provided doesn't, in fact, touch the MO after releasing the
MOC.
Pleas reconsider my first tip and let the calling method create the
NSManagedObjectContext. Use that to fetch all your objects, process
them and _then_ kill your NSManagedObjectContext.
An NSManagedObject can not live without its NSManagedObjectContext.
The NSManagedObjectContext ist the bucket that holds it. You can not
take it out and throw the bucket away. Will not work.
A good practice, and something the OP is actually doing. As long as
each property is being copied as the OP indicated, the dictionary
*only* contains copies of managed object properties, which are
therefore owned by the caller and *not* by the MO or MOC. So,
releasing the context should not make any difference, as it is
released after the MO is processed (by coping its properties) as you
recommend.
Am 01.05.2009 um 10:24 schrieb Daniel Kennett:
NSManagedObjectContext *context = [[NSManagedObjectContext alloc]
init];
Pet *pet = [KNClarusQuickDocumentParser petAtURL:url
inContext:context];
NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
[dict setValue:[[[pet valueForKey:@"name"] copy] autorelease]
forKey:@"name"];
[dict setValue:[[[pet valueForKey:@"birthday"] copy] autorelease]
forKey:@"birthday"];
// More copying of strings and dates
[..]
[dict setValue:[[[pet valueForKey:@"pertinentActions"] copy]
autorelease] forKey:@"pertinentActions"]; // <--- Here
[context release];
^^^^^^^^^^^^^^^^^ this release kills all fetched objects.
Very true.
return [dict autorelease];
^^^^^^^^^^^^^^^^^ this dict contains dead objects.
Probably false, unless there is missing code that violates the rule.
In any case, this issue is likely irrelevant based on when the crash
occurs and for what object(s).
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
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