UIViewController保留问题:count永远不会达到零 [英] UIViewController retain problem: count never reaches zero

查看:92
本文介绍了UIViewController保留问题:count永远不会达到零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请看下面的代码。这部分从堆栈中弹出顶视图控制器(通常是相同的 ArticleControllerController )(我发现问题保持不变,无论我弹出单个控制器还是弹出到根目录控制器),创建新的并添加到堆栈。

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.

问题是,它的保留计数永远不会变为零,因此 dealloc ArticleControllerController 的方法,不会释放大量的各种接口对象。最终应用程序崩溃(至少在设备中,至少我认为这部分是主要问题),因为内存不足。

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
} 

所以,我想,popViewControllerAnimated只释放剩下的一个两个保留。为什么?我应该寻找什么?我能做什么?拨打 [ac release] 两次(这可能是件很糟糕的事情)?

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)?

推荐答案

您如何知道导航控制器的保留计数为2是不正确的行为?

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

您不能依赖retainCount来执行alloc / release调试因为我们不知道UIKit的内部是如何工作的。只要您在代码中保留并正确释放,您就可以99.9%确定UIKit也能正常工作。

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.

我猜想popViewController将删除所有保留pushViewController添加了 - 即使我不知道会有多少 - 但它可能会将其设置为自动释放,因此您不能保证它会在调用popViewController后立即释放。

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.

我假设UIKit导航控制器没有错误(否则许多其他开发人员会抱怨它!)并且在你的代码中某处你将它保留在某个地方else(可能没有意识到它,即声明保留而不是分配的委托属性等)

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)

希望有所帮助!

这篇关于UIViewController保留问题:count永远不会达到零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆