问题
I have a navigation app with 3 view controllers on the stack. The navigation root pushes AvailableItemsViewController, passing a managed object context to it.
This view, which is a table view that uses a fetched results controller to populate it, has an add button which pushes another controller (CreateNewItemViewController) I pass that context off to it.
In CreateNewItemViewController I create a managed object, save it to the context, then pop the view controller.
I can go back and forth saving items to the AvailableItemsViewController until I go back to the navigation root. (The AvailableItemsViewController gets deallocated).
After this if I drill back down to the third controller and try to save I get this:
Here's my error (zombies enabled:)
-[AvailableItemsViewController controllerWillChangeContent:]: message sent to deallocated instance 0x1f6500
and without zombies
-[__NSArrayM controllerWillChangeContent:]: unrecognized selector sent to instance 0x4ecdee0
(i've gotten a lot of different random output on this, it seems that the destination of the controllerWillChangeContent: message is random)
all controllers are setting their properties to nil and releasing ivars in viewDidUnload and dealloc respectively.
When stepping through the crash happens at [self.managedObjectContext save:&error] in the third controller (CreateNewItemViewController)
回答1:
As mentioned above, make sure your FRC is not leaking. However, if the problem persists, set the FRC delegate to nil in your controller's dealoc. This will disable change tracking and prevent calls to your deallocated VC.
回答2:
You're leaking the NSFetchedResultsController. This means that it stays alive, and when something changes the data store it does its job and tries to signal the change. But since the delegate has been deallocated, you get EXC_BAD_ACCESS or messages sent to whatever random object happens to have been allocated at that memory location.
来源:https://stackoverflow.com/questions/6025763/deallocated-view-controller-causing-exc-bad-access-because-of-fetched-results-co