Why is this object freed?

I am developing an application for the iPhone, I am trying to call a view into the navigation controller, which I did many times before, however, I am having some problems with this particular application. I have a table view, and when the user selects one row, a new view is inserted into the controller:

DataWrapper *row=[[self.rows objectAtIndex:[indexPath section]] objectAtIndex:[indexPath row]]; DataViewController *nextController=[[DataViewController alloc] initWithNibName:@"Data" bundle:[NSBundle mainBundle]]; [nextController setInfo:row]; [nextController setRow:[indexPath row]]; [nextController setParent:self]; [self.navigationController pushViewController:nextController animated:YES]; [nextController release]; 

and everything will be fine until the user closes the back button, I get an exception and use NSZombieEnabled and get the following:

 -[DataViewController respondsToSelector:]: message sent to deallocated instance 0x4637a00 

So, I tried to remove the [nextController release] and it actually worked, but WHY ???? I highlighted nextController, so I have to release it, right? I do not feel right releasing this application, if there is something like this, I feel that it will fail. Please let me know your thoughts.

+4
source share
5 answers

Your nextController not saved by the navigation controller. If you release it because there is only one init/release pair, the object is freed. Later, when the navigationController tries to send messages to it, you will receive an error message.

This is why removing [nextController release] fixes the problem.

You are right that if you set aside, you must free him. But the caveat is only after your application is done with it, and not earlier.

Some objects will remain highlighted for almost the entire life of the application, so don’t feel too bad.

+5
source

I would suggest that [self.navigationController] returns nil , because if it was not nil , it would save your object. Since your object is not saved, it seems that there is no object trying to save it, indicating that the navigationController property is empty.

0
source

Is it possible that your navigation controller is somehow freed, as a result of which view managers are also freed? Perhaps you can test it by saving the nav controller before clicking nextController .

0
source

For debugging purposes, I would override -dealloc in your DataViewController class and set a breakpoint on it.

0
source

but it works for something like:

 DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"SomeView" bundle:nil]; controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve; [self presentModalViewController: controller animated:NO]; [controller release]; 

so why doesn't it work for pushViewController?

0
source

All Articles