Why might releasing a managed object crash in -[_PFManagedObjectReferenceQueue _queueForDealloc:]?

QueueForDealloc: is an undocumented internal method. It shows up in stacks from time to time but it's nothing we deal with directly Your problem is most likely caused by over-releasing a managedObject. A managedObject will be strongly retained by a context that inserts, updates or changes the object so if you micromanage the objects own retention, you can over-release it prior to the context releasing it.

This cause the managed object to disappear at seemingly at random. Conversely, you can over-retain causing an object to persist after the context has deleted it I encourage people to avoid retaining managed objects but when you do, put them a class property or a collection like an array or set. That way, the retention is handled for you.

QueueForDealloc: is an undocumented internal method. It shows up in stacks from time to time but it's nothing we deal with directly. Your problem is most likely caused by over-releasing a managedObject.

A managedObject will be strongly retained by a context that inserts, updates or changes the object so if you micromanage the objects own retention, you can over-release it prior to the context releasing it. This cause the managed object to disappear at seemingly at random. Conversely, you can over-retain causing an object to persist after the context has deleted it.

I encourage people to avoid retaining managed objects but when you do, put them a class property or a collection like an array or set. That way, the retention is handled for you.

Clearly from the stack trace, -release is being called, deciding therefore it's time to deallocate the object (I assume Core Data will do this when reaching a retain count of zero like the rest of Cocoa). But then as part of that deallocation routine, Core Data deallocates the object and then sends it a message? That just doesn't make sense!

I don't think I'm looking at your run-of-the-mill memory management error – Mike Abdullah Mar 8 at 23:11 I think the context still has a reference to a managedObject that was killed in a prior drain owing to overrelease. The context still thinks the object is alive and puts a reference to it in the queue.It then tries to send the various delete and deallocation methods but the object is no longer at the memory address. Since "unmanaging" a managedObject and removing it from the object graph is a complex process with many side effects, the improperly killed objects causes a crash depending on the specific state of the graph at anyone one time.

– TechZen Mar 9 at 14:56 To test my idea, remove any releases/autoreleases of the managedObject in code and see if the problem goes away. If it does, readd the releases until the problem comes back. – TechZen Mar 9 at 14:57 That's not really feasible.

I have all manner of controllers that retain managed objects as part of their work. I know memory management inside out and don't think this is a simple over-release – Mike Abdullah Mar 9 at 15:52 Fair enough. I can only tell you what I have found when myself and others have had a similar error.

I think that the function of -_queueForDealloc: is obvious by it's name.It is reasonable to assume that for some reason, an object in the queue for deallocation is causing the problem. Why that is, I can't say. Do your managedObjects hold references to any non-ManagedObjects or do they carry out some kind of side effect operations?

If it's not retention of something like those, I've got nothing. – TechZen Mar 9 at 21:11.

Having finally been able to reproduce the problem on a development machine, it seems this crash is a side-effect of an earlier exception during context teardown. The sequence of events is something like: The MOC is being deallocated, so it's time to tear down its contents To do so, all registered MOs are turned into faults* The act of turning a MO into a fault sends KVO-notifications An observer receives the notification and tries to act upon it, hitting a now invalid MO in the graph Core Data throws an exception from the invalid access For reasons unknown, that exception is not passed to my exception reporter The MOs get released, but the exception left Core Data in an unexpected state, so the MO deallocation crashes In short the real problem is that observers outlive the context; don't allow them too! Any object observing a MO should probably also have a strong reference to the MOC, like NSObjectController and friends do.

*I found in testing that Core Data often does this on a background thread, presumably to avoid blocking the main thread MOC – managed object context MO – managed object.

NSManagedObject uses dynamic class generation to support the Objective-C 2 properties feature (see “Declared Properties”) by automatically creating a subclass of the class appropriate for entity. InitWithEntity:insertIntoManagedObjectContext: therefore returns an instance of the appropriate class for entity. The dynamically-generated subclass will be based on the class specified by the entity, so specifying a custom class in your model will supersede the class passed to alloc.

If context is not nil, this method invokes context insertObject:self (which causes awakeFromInsert to be invoked). You are discouraged from overriding this method—you should instead override awakeFromInsert and/or awakeFromFetch (if there is logic common to these methods, it should be factored into a third method which is invoked from both). If you do perform custom initialization in this method, you may cause problems with undo and redo operations.

In many applications, there is no need to subsequently assign a newly-created managed object to a particular store—see assignObject:toPersistentStore:. If your application has multiple stores and you do need to assign an object to a specific store, you should not do so in a managed object's initializer method. Such an assignment is controller- not model-level logic.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions