ViewController never gets deallocated

孤街醉人 提交于 2019-12-11 11:07:27

问题


In my mind, myViewController should be deallocated around the time that I pop back to the root view controller with the following code, but I never see the deallocation message getting NSLogged.

If this should work, then what kind of problem can I look for in the myViewController's class that might cause it to get deallocated when I popToRootViewController?

Thanks.

The following gets called in my tableView:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

MyViewController *vc = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];

[self.navigationController pushViewController:vc animated:YES];

[vc release];
}

UPDATE:

This code was perfect, but it was some bad memory management in my custom view controllers that caused neither to be released. I had some retained properties that should have been assign instead (or at least, that's the way I solved it). See comments for specifics.


回答1:


When you use poptoviewcontroller then dealloc method will call for the topmost view controller in navigation controller. You can put a breakpoint in dealloc method of your current view controller and when you called popviewcontroller then your dealloc method gets called and release all the stuff/varaibles you have created in your view controller.




回答2:


@JaySmith02 is right

it was some bad memory management in my custom view controllers that caused neither to be released. I had some retained properties that should have been assign instead (or at least, that's the way I solved it)

In my case the culprit was

@property (nonatomic, retain) id<TTSlidingPagesDataSource> dataSource;

From my viewController when I wrote

slidingPages.dataSource = self

I guess the dataSource retained my viewController and made a circular retention. The 'dealloc' of my viewController was never getting called.

Note: Under ARC dealloc does get called. Difference is you cannot call [super dealloc]

The solution:

@property (nonatomic, assign) id<TTSlidingPagesDataSource> dataSource;


来源:https://stackoverflow.com/questions/8647044/viewcontroller-never-gets-deallocated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!