Re: Crashing resetting or releasing an NSManagedObjectContext
Well, that was quick. With NSZombieEnabled, I get this when deallocating my context: *** -[VetVisit_VetVisit_ _hasRetainedStoreResources]: message sent to deallocated instance 0x16b85600 VetVisit is class that represents an Entity in my object model - the Pet entity has a collection of VetVisit objects. I don't actually ever manage these directly - I let Core Data NSArrayControllers handle them. In this case, the model is never seeing any array controllers. Is this something wrong with my model? I can't see anything wrong at surface level - indeed, the app itself has been working perfectly with its model for some time. I'd appreciate any pointers. You've over released a managed object. If you use Instruments ObjectAlloc tool with retain counting and Zombies enabled it will record each stack trace of every retain and release. It makes these things a lot easier to track down. - Ben ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext [SOLVED]
*Sigh* All this effort, and it turns out to be a one-line fix. Obviously. [NSManagedObjectContext -setRetainsRegisteredObjects:YES] Calling that on the context solves all the problems I was having, and everything works perfectly now! Thanks, -- Daniel ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in any reply you send. On 5 May 2009, at 09:46, Daniel Kennett wrote: Thank you again for your helpful replies! On a different note, when you comment out the for (VetVisit code, does it still crash on a VetVisit, or does it crash on a Medication object? It still crashes on a VetVisit object. I don't think I've solved the problem, but I have stopped it crashing. Putting a breakpoint on [VetVisit -release] showed me that NSFastEnumeration was releasing the object: for (VetVisit *visit in [self vetVisits]) { // -- Stack trace for [VetVisit -release] comes from here [visit className]; } I solved the crash by doing this: NSArray *visits = [[self vetVisits] allObjects]; for (VetVisit *visit in visits) { [visit className]; } ... and now it works fine! The accessors for the vetVisits set are just the ones made by the Core Data wizard thing - they're declared like this: @property (nonatomic, retain) NSSet* vetVisits; ... and implemented with this: @dynamic vetVisits; So, I've stopped the crash, but I'm thoroughly confused about what's going on and think I'm just papering over the problem. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext
Thank you again for your helpful replies! On a different note, when you comment out the for (VetVisit code, does it still crash on a VetVisit, or does it crash on a Medication object? It still crashes on a VetVisit object. I don't think I've solved the problem, but I have stopped it crashing. Putting a breakpoint on [VetVisit -release] showed me that NSFastEnumeration was releasing the object: for (VetVisit *visit in [self vetVisits]) { // -- Stack trace for [VetVisit -release] comes from here [visit className]; } I solved the crash by doing this: NSArray *visits = [[self vetVisits] allObjects]; for (VetVisit *visit in visits) { [visit className]; } ... and now it works fine! The accessors for the vetVisits set are just the ones made by the Core Data wizard thing - they're declared like this: @property (nonatomic, retain) NSSet* vetVisits; ... and implemented with this: @dynamic vetVisits; So, I've stopped the crash, but I'm thoroughly confused about what's going on and think I'm just papering over the problem. Thanks, -- Daniel ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in any reply you send. On 4 May 2009, at 23:44, Keary Suska wrote: On May 1, 2009, at 2:24 AM, Daniel Kennett wrote: Thanks for your reply. Yes, I have code that triggers relationship faults, and removing that code solves the problem. However, I need that code to work! :-) When fetching the data from the object tree, I call a method on the pet instance called -pertinentActions. This method loops through various relationships, calling -pertinentAction on each child object. I've ruled out the -pertinentAction method, since calling - className on the child objects also causes the crash to happen. I've also double and triple checked my retains and (auto)releases and they're all balanced. My recommendation is don't check them *prove* them. If you release or autorelease any vetvisit object, comment it out and see if the problem goes away. I say this because I have had, and just about any programmer probably has, stared at the exact glaring error over and over again and not seen it. In any case, there is an overrelease happening, which means somewhere retains are *not* balanced, and 99.% of the time, it's the programmer's code. But you have to find out how it is happening, so it's time to bring out Instruments--it can tell you who is the culprit and where it is. On a different note, when you comment out the for (VetVisit code, does it still crash on a VetVisit, or does it crash on a Medication object? ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext
Good morning all, Thanks for your reply. Yes, I have code that triggers relationship faults, and removing that code solves the problem. However, I need that code to work! :-) When fetching the data from the object tree, I call a method on the pet instance called -pertinentActions. This method loops through various relationships, calling -pertinentAction on each child object. I've ruled out the -pertinentAction method, since calling -className on the child objects also causes the crash to happen. I've also double and triple checked my retains and (auto)releases and they're all balanced. Here's the code that triggers the crash: -(NSArray *)pertinentActions { NSMutableArray *actions = [[NSMutableArray alloc] init]; [actions addObject:[self birthdayAction]]; for (InsurancePolicy *policy in [self insurancePolicies]) { KNClarusPertinentAction *action = [policy pertinentAction]; if (action) { [actions addObject:action]; } } // If I return here, the later crash doesn't happen! for (VetVisit *visit in [self vetVisits]) { [visit className]; } // Returning just after this... for (Medication *medication in [self medications]) { [medication className]; } // ... or this causes the later crash. return [actions autorelease]; } Commenting out the for (VetVisit* and for (Medication* loops fixes the crash. At this point, I'm doing no memory management at all - the - pertinentActions method is being called thusly: 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]; return [dict autorelease]; I think I've stumbled upon a problem with the way I've set up my model. The insurance policy collection, which doesn't crash after being accessed, is simply a one-to-many relationship from pet. However, the Medication and VetVisit relationships are a little more complex. It goes: Pet - Medication(s) - Medication Course(s) - Medication Dose(s) Pet - Vet Visit(s) The Medication Dose entity has an optional relationship to Vet Visit in case the dose was given at a visit to the vet. Here's a screenshot of that part of my model: http://www.kennettnet.co.uk/stuff/ClarusModel.png Right now, I'm at a dead-end. I've ruled out my memory management, and can't see how I can work around what's happening, or what I'm doing wrong! Thanks, -- Daniel ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in any reply you send. Does the Pet class, or any class it has a relationship with, release or autorelease any VetVisit object? I am assuming that the Pet entity has a relationship to VetVisit--do you have any code that would cause the relationship fault to fire? If so, what happens when you comment that out? 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
Re: Crashing resetting or releasing an NSManagedObjectContext
You are still having the same error. 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. 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. atze 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. return [dict autorelease]; ^ this dict contains dead objects. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext
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 (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext
On May 1, 2009, at 2:24 AM, Daniel Kennett wrote: Thanks for your reply. Yes, I have code that triggers relationship faults, and removing that code solves the problem. However, I need that code to work! :-) When fetching the data from the object tree, I call a method on the pet instance called -pertinentActions. This method loops through various relationships, calling -pertinentAction on each child object. I've ruled out the -pertinentAction method, since calling - className on the child objects also causes the crash to happen. I've also double and triple checked my retains and (auto)releases and they're all balanced. My recommendation is don't check them *prove* them. If you release or autorelease any vetvisit object, comment it out and see if the problem goes away. I say this because I have had, and just about any programmer probably has, stared at the exact glaring error over and over again and not seen it. In any case, there is an overrelease happening, which means somewhere retains are *not* balanced, and 99.% of the time, it's the programmer's code. But you have to find out how it is happening, so it's time to bring out Instruments--it can tell you who is the culprit and where it is. On a different note, when you comment out the for (VetVisit code, does it still crash on a VetVisit, or does it crash on a Medication object? Here's the code that triggers the crash: -(NSArray *)pertinentActions { NSMutableArray *actions = [[NSMutableArray alloc] init]; [actions addObject:[self birthdayAction]]; for (InsurancePolicy *policy in [self insurancePolicies]) { KNClarusPertinentAction *action = [policy pertinentAction]; if (action) { [actions addObject:action]; } } // If I return here, the later crash doesn't happen! for (VetVisit *visit in [self vetVisits]) { [visit className]; } // Returning just after this... for (Medication *medication in [self medications]) { [medication className]; } // ... or this causes the later crash. return [actions autorelease]; } Commenting out the for (VetVisit* and for (Medication* loops fixes the crash. At this point, I'm doing no memory management at all - the -pertinentActions method is being called thusly: 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]; return [dict autorelease]; I think I've stumbled upon a problem with the way I've set up my model. The insurance policy collection, which doesn't crash after being accessed, is simply a one-to-many relationship from pet. However, the Medication and VetVisit relationships are a little more complex. It goes: Pet - Medication(s) - Medication Course(s) - Medication Dose(s) Pet - Vet Visit(s) The Medication Dose entity has an optional relationship to Vet Visit in case the dose was given at a visit to the vet. Here's a screenshot of that part of my model: http://www.kennettnet.co.uk/stuff/ClarusModel.png Right now, I'm at a dead-end. I've ruled out my memory management, and can't see how I can work around what's happening, or what I'm doing wrong! Does the Pet class, or any class it has a relationship with, release or autorelease any VetVisit object? I am assuming that the Pet entity has a relationship to VetVisit--do you have any code that would cause the relationship fault to fire? If so, what happens when you comment that out? 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
Re: Crashing resetting or releasing an NSManagedObjectContext
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 ___ dan...@kennettnet.co.uk 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
Re: Crashing resetting or releasing an NSManagedObjectContext
Hi again, Well, that was quick. With NSZombieEnabled, I get this when deallocating my context: *** -[VetVisit_VetVisit_ _hasRetainedStoreResources]: message sent to deallocated instance 0x16b85600 VetVisit is class that represents an Entity in my object model - the Pet entity has a collection of VetVisit objects. I don't actually ever manage these directly - I let Core Data NSArrayControllers handle them. In this case, the model is never seeing any array controllers. Is this something wrong with my model? I can't see anything wrong at surface level - indeed, the app itself has been working perfectly with its model for some time. I'd appreciate any pointers. Thanks, -- Daniel ___ dan...@kennettnet.co.uk 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 ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in any reply you send. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list.
Re: Crashing resetting or releasing an NSManagedObjectContext
On Apr 30, 2009, at 10:55 AM, Daniel Kennett wrote: Hi again, Well, that was quick. With NSZombieEnabled, I get this when deallocating my context: *** -[VetVisit_VetVisit_ _hasRetainedStoreResources]: message sent to deallocated instance 0x16b85600 VetVisit is class that represents an Entity in my object model - the Pet entity has a collection of VetVisit objects. I don't actually ever manage these directly - I let Core Data NSArrayControllers handle them. In this case, the model is never seeing any array controllers. Is this something wrong with my model? I can't see anything wrong at surface level - indeed, the app itself has been working perfectly with its model for some time. I'd appreciate any pointers. Does the Pet class, or any class it has a relationship with, release or autorelease any VetVisit object? I am assuming that the Pet entity has a relationship to VetVisit--do you have any code that would cause the relationship fault to fire? If so, what happens when you comment that out? 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 ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in
Crashing resetting or releasing an NSManagedObjectContext
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 ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in any reply you send. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext
On Apr 29, 2009, at 2:59 AM, Daniel Kennett wrote: 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! The most common cause if this error is an over-release. Turn on zombies and see who (if any) is being over-released. The culprit could be in your copy data code, that you don't provide. There doesn't seem to be anything obviously wrong (other than explained below), that I can see. Is there a good example anywhere of how to set up and tear down a Core Data document correctly? I can say that the design you present is flawed according to OOP rules, specifically how you release the managed object context. An object should never release an object it doesn't own or has retained. I understand why you are doing this--I am just saying it is a flawed approach. I don't know what the KNClarusQuickDocumentParser class does in its entirety, but it may call for a singleton pattern or at least a shared object pattern (not a proper pattern name AFAIK, just mentioning the technique, for instance, [NSNotificationCenter defaultCenter]). Tear down, in your case, should be as simple as releasing the MOC. The other calls (-reset etc) are unnecessary. Keary Suska Esoteritech, Inc. Demystifying technology for your home or business ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: Crashing resetting or releasing an NSManagedObjectContext
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 ___ dan...@kennettnet.co.uk http://www.kennettnet.co.uk Please include previous messages in any reply you send. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/atze%40freeport.de This email sent to a...@freeport.de ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com