释放在viewDidUnload和dealloc两者? [英] Release in viewDidUnload and dealloc both?
问题描述
我已经有一段时间的假设,当控制器被释放时,总是调用viewDidUnload
.这是正确的假设吗?
I have been under the assumption for a while that viewDidUnload
is always called when a controller is deallocated. Is this a correct assumption?
我一直在探索一些奇怪的东西,并在控制器的viewDidUnload
和dealloc
中设置一个断点.似乎已调用dealloc
,但从未调用viewDidUnload
方法.我什至在dealloc
中添加了self.view = nil
,但它似乎仍然没有被调用.
I've just been exploring some odd things, and set a breakpoint in my controller's viewDidUnload
and it's dealloc
. It appears that dealloc
is called, but the viewDidUnload
method is never called. I even added a self.view = nil
to my dealloc
and it still didn't seem to call it.
这是否意味着我还需要在我的dealloc
方法中释放已通过viewDidUnload
方法释放的保留视图对象,以确保它们确实消失了?
Does this mean that retained view objects I have been releasing in the viewDidUnload
method also need to be released in my dealloc
method to be sure they really go away?
我知道StackOverflow上还有很多关于viewDidUnload
的问题,但是没有一个问题专门解决关于这两种方法之间的发布语句重复的问题.
I know there are many other questions on StackOverflow about viewDidUnload
, but none specifically address this issue about duplication of release statements between the 2 methods.
在3.1.2 SDK上的新项目中,更具体的示例:
A more concrete exmaple in a fresh project on the 3.1.2 SDK:
@implementation TestViewController
@synthesize label;
- (IBAction)push {
TestViewController *controller = [[[TestViewController alloc] initWithNibName:@"TestViewController" bundle:nil] autorelease];
[self.navigationController pushViewController:controller animated:YES];
}
- (void)viewDidUnload {
self.label = nil;
NSLog(@"viewDidUnload was called");
}
- (void)dealloc {
[super dealloc];
NSLog(@"label retain count: %i", [label retainCount]);
}
@end
我的应用程序委托使用其其中之一作为根控制器来创建一个简单的导航控制器.当我点击链接到push
的按钮3次,然后再单击返回按钮3次时,将生成以下输出.
My app delegate creates a simple navigation controller with one of these as it's root controller. When I tap the button linked to push
3 times, and then hit the back button three times, the following output is generated.
ViewDidUnloadTest[2887:207] label retain count: 2
ViewDidUnloadTest[2887:207] label retain count: 2
ViewDidUnloadTest[2887:207] label retain count: 2
比我认为的高2倍.由视图保留一次,由控制器保留一次.但是,在dealloc
之后,我曾希望视图可以释放我的标签,而控制器将可以调用viewDidUnload
并释放它.尽管此时可能会有autorelease
可以取消计数.
Which is 2 higher that I would think it would be. Retained once by the view and once by the controller. But after the dealloc
I would have expected the view to be gone releasing my label, and the controller to be gone calling viewDidUnload
and releasing it. Although there may be an autorelease
in there throwing off the count at this point.
但是至少很明显,根本没有调用viewDidUnload
,这与这里的答案相反:
But at least it's clear that viewDidUnload
is not getting called at all, which contrary to this answer here: Are viewDidUnload and dealloc always called when tearing down a UIViewController?
也许我应该在控制器上的所有dealloc方法中简单地调用[self viewDidUnload]
?更糟糕的是,我将一个属性设置为nil两次,对吧?
Perhaps I should simply call [self viewDidUnload]
in all my dealloc methods on controllers? Worse than can happen is that I set a property to nil twice, right?
推荐答案
除非需要打破保留周期,否则通常 仅在dealloc
方法中释放对象. viewDidUnload
是一个例外;在内存不足的情况下调用它,并且应使用它来释放所有可能的东西.
Unless you need to break a retain cycle, you should generally only be releasing objects in your dealloc
method. viewDidUnload
is an exception; it is invoked in low memory situations and should be used to release anything that you can.
如果确实需要将它们释放到其他位置,请始终在release
之后将引用设置为nil.这样可以保护您的应用程序免于以后崩溃(可能在dealloc
中).
If you do need to release them anywhere else, then always set the reference to nil after the release
. That'll protect your app from blowing up later (likely in dealloc
).
请注意,文档非常明确地指出,调用viewDidUnload
时,view属性已经为nil.
Note that the documentation quite explicitly calls out that the view property will already be nil when viewDidUnload
is called.
这篇关于释放在viewDidUnload和dealloc两者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!