开发者

Why is this object being deallocated?

开发者 https://www.devze.com 2022-12-12 04:55 出处:网络
I\'m developing an iPhone app, I\'m trying to push a view into the navigation controller, which I\'ve done many times before, however, I\'m having some issues with this particular app. I have a table

I'm developing an iPhone app, I'm trying to push a view into the navigation controller, which I've done many times before, however, I'm having some issues with this particular app. I have a table view, and when the user selects one row the new view is pushed 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 rel开发者_运维百科ease];

and it goes fine, until the user taps the back button, I get an exception, and used NSZombieEnabled and get this:

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

So i tried to remove the [nextController release] and in fact it worked, but WHY???? I allocated nextController, so I'm supposed to release it, right?? I don't feel right releasing this app if there's something like this, I feel like it's going to fail. Please let me know your thoughts.


Your nextController isn't being retained by navigation controller. If you release it then because there is only one init/release pair, the object is deallocated. Later when the navigationController attempts to send messages to it, you get the error you see.

This is also why remove [nextController release] fixes the problem.

You are right in that if you allocated, you should free it. But the caveat is only after your application is done with it, not before.

Some objects will stay allocated for nearly the lifetime of the application, so don't feel too bad.


I would guess that [self.navigationController] is returning nil, because if it weren't nil, it would be retaining your object. Since your object is not getting retained, it would appear that there is no object that's trying to retain it, indicating that the navigationController property is empty.


Is it possible that your navigation controller is somehow being deallocated, resulting in the view controllers also getting released? You could maybe test it by retaining the nav controller just before pushing nextController.


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


but it seems to work for something like:

DetailViewController *controller = [[DetailViewController alloc] initWithNibName:@"SomeView" bundle:nil]; 

controller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController: controller animated:NO];

[controller release];

so, why is it not working for pushViewController ?

0

精彩评论

暂无评论...
验证码 换一张
取 消