If I release, I get bad access, if I retain, I leak

I have a view controller that i’m trying to push onto the navigation stack. I create the controller with a local variable, set it up, then assign it to a property. Then if I release the variable, I get EXE_BAD_ACCESS. if I retain (or auto release) it leaks.

CustomTVC* controller = [[CustomTVC alloc]initWithStyle:UITableViewStyleGrouped];
controller.managedObjectContext = self.managedObjectContext;

self.tableViewControllerIvar = controller;

[self.navigationController pushViewController:self.tableViewControllerIvar animated:YES];
 //[controller autorelease]; or [controller release]; or nothing

Here is exactly what happens if I release

  • Siri always responds 'Continue in the app' when starting workout with SiriKit
  • Red banner comes and disappear suddenly when press home button during recording on going
  • NSArray becomes NSCFArray when passed
  • Get DNS server IP from iphone settings
  • Proper way of saving and loading pictures
  • How Can I Start And Stop NSTimer?
    1. The above code is fired from pushing an add button in the nav bar.
    2. the view is pushed and everything is fine. In the new view I can push more views in over and over with no problem…unless
    3. I go back to the root view of the navigation stack. (Which is where the above code is from).
    4. Now if I drill down again to the second view, then try to push another it crashes.

    Edit: I have a feeling that something is going wrong when I push the third controller onto the stack. With the push, it’s inserting a new object into the managed object context which is causing the fetchedresultscontroller to update the tableview. There may be a broken pointer in there somewhere. I’ll play with it and post the results. –

    Edit: 5/16

    Getting this error message in the log

    * -[CustomTVC controllerWillChangeContent:]: message sent to deallocated instance 0x187270

    This only happens after I pop the CustomTVC off the stack (go back to the navigation root view controller) I can push and save all I want as long as I don’t pop the CustomTVC.

    3 Solutions Collect From Internet About “If I release, I get bad access, if I retain, I leak”

    Fixed it. Had to set the fetched results controllers delegate to nil in viewDidLoad.

    - (void)dealloc
    {
        self.fetchedResultsController.delegate = nil;
        [_fetchedResultsController release];
        [_managedObjectContext release];
        [super dealloc];
    }
    

    seems the culprit was(according to the zombie instruments):

    [NSFetchedResultsController(private methods) _managedObjectContextDidChange:]

    Edit(s): Finally took the time to figure out how to put code in here correctly (I’m lazy)

    autorelease should get the job done. When you assign tableViewControllerIvar, you can just call [controller autorelease]. This should take care of that.

    CustomTVC* controller = [[CustomTVC alloc]initWithStyle:UITableViewStyleGrouped];
    controller.managedObjectContext = self.managedObjectContext;
    
    self.tableViewControllerIvar = [controller autorelease];
    
    [self.navigationController pushViewController:self.VCTVC animated:YES];
    

    If you are still getting EXE_BAD_ACCESS, then something else must be going on. Have you absolutely confirmed that this code, when run more than once, is causing the bad access?

    you are assigning controller to tableViewControllerIvar

     self.tableViewControllerIvar = controller;
    

    and you are releasing controller, so you need to retain it in the above line

     self.tableViewControllerIvar = [controller retain];
    

    and when you are done with your “tableViewControllerIvar” then just release it, you shall not get any leaks after that