Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.1k views
in Technique[技术] by (71.8m points)

iphone - 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

  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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...