UIViewController retain problem: count never reaches zero

ε祈祈猫儿з 提交于 2019-12-11 05:05:23

问题


Please, take a look at my code bellow. This part pops top view controller (usually, the same ArticleControllerController) from the stack (I found that the problem stays the same no matter if I pop single controller or pop to the root controller), creates new one and adds to the stack.

The problem is, that its retain count never goes to zero and so dealloc method of ArticleControllerController is never called leaving large amounts of various interface objects unreleased. Eventually app crashes (at least in device and at least I think this part is the main problem) because of low memory.

- (void) navigateToNewsCategoryByIndex:(int)idx {
    [app.nav popViewControllerAnimated:NO]; // could be popToRootController
    ArticleControllerController *ac = [[ArticleControllerController alloc] init];
    ac.categoryIndex = idx;
    [app.nav pushViewController:ac animated:NO];
    [ac release];
    NSLog(@"AC retain count: %d", [ac retainCount]); // prints 2
} 

So, I guess, popViewControllerAnimated releases only one of the remaining two retains. Why? What should I look for? What can I do? Call [ac release] two times (that would be terrible thing)?


回答1:


How do you know that a retain count of 2 isn't correct behavior for a navigation controller?

You can't rely on retainCount to do alloc / release debugging because we don't know how the internals of the UIKit work. As long as you retain and release correctly inside your code you can be 99.9% sure that UIKit will also be working correctly.

I would guess that popViewController will remove all the retains that pushViewController added - even though I don't know how many that would be - but it might set it to be autoreleased so you can't guarantee that it will be released immediately after a call to popViewController.

I'd work on the assumption that the UIKit navigation controller doesn't have a bug (otherwise lots of other developers would be complaining about it!) and somewhere in your code you're retaining it somewhere else (probably without realising it i.e. a delegate property declared to retain instead of assign etc)

Hope that helps!



来源:https://stackoverflow.com/questions/3905702/uiviewcontroller-retain-problem-count-never-reaches-zero

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