2

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)

bneely
  • 9,083
  • 4
  • 38
  • 46
Myron Slaw
  • 886
  • 9
  • 21

2 Answers2

3

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.

Scott Ahten
  • 1,141
  • 7
  • 15
  • Yeah this is it, I actually figured it out yesterday. Instruments in the Lion release of Xcode is broken atm so I moved the project to another computer to use zombies. Found that something in core data was still sending messages to the old view controller, so I figured it was the FRC delegate. http://stackoverflow.com/questions/6006028/if-i-release-i-get-bad-access-if-i-retain-i-leak – Myron Slaw May 18 '11 at 23:11
  • Also useful when using ARC. Had similar issue and tried to nil the delegate in viewWillDissappear. Had better luck in dealloc ! Thanks a lot! – FeltMarker Aug 14 '12 at 13:23
0

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.

Anomie
  • 92,546
  • 13
  • 126
  • 145
  • So is there something i'm supposed to be dong in dealloc besides releasing the ivars? (and view did unload) – Myron Slaw May 17 '11 at 03:13
  • I have both being released and set to nil in dealloc and viewDidUnload – Myron Slaw May 17 '11 at 03:15
  • @LoneGunman: But are you managing the memory correctly when you allocate it? If you're working through ivars, are you making sure to release the old value before assigning a new one? – Anomie May 17 '11 at 03:25