xcode 未调用 UIView 子类的 Dealloc 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5438734/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dealloc method of UIView subclass not getting called
提问by iHS
I have a view controller named SettingsViewController, and a custom UIView subclass named ViewAccounts. In SettingsViewController, I am using the code below to add ViewAccounts view into SettingsViewController.
我有一个名为 SettingsViewController 的视图控制器和一个名为 ViewAccounts 的自定义 UIView 子类。在 SettingsViewController 中,我使用下面的代码将 ViewAccounts 视图添加到 SettingsViewController 中。
if(!self.viewAccounts)
{
ViewAccounts *objViewAccounts= [[ViewAccounts alloc] initWithFrame:CGRectMake(0, yViews, 320, 400) withViewController:self];
[self.view insertSubview:objViewAccounts atIndex:0];
self.viewAccounts = objViewAccounts;
[objViewAccounts release];
}
The Problem is that the dealloc method of ViewAccounts (subclass of UIView) is not getting called. If I comment these both lines of code, then dealloc method gets called.
问题是 ViewAccounts(UIView 的子类)的 dealloc 方法没有被调用。如果我注释这两行代码,则调用 dealloc 方法。
[self.view insertSubview:objViewAccounts atIndex:0];
self.viewAccounts = objViewAccounts;
I guess the problem is with retain count, at it gets incremented by 1 when we use insertSubview or addSubiew, but how to get rid of it. Is this the right approach to use it?
我想问题在于保留计数,当我们使用 insertSubview 或 addSubiew 时,它会增加 1,但是如何摆脱它。这是使用它的正确方法吗?
回答by Andy S.
Just as a note: making viewAccountto assign can cause a Zombie down the road (in the original code snippet) because you release it in the last line.
请注意:让viewAccount分配可能会导致僵尸(在原始代码片段中),因为您在最后一行释放它。
I would suggest the following approach:
我建议采用以下方法:
- Make viewAccounts retainagain
- Don't use autorelease because you are not returning it from that method
- Pay attention to deallocmethod of the method in the snippet
- 使 viewAccounts再次保留
- 不要使用 autorelease,因为你不是从那个方法返回它
- 注意snippet中方法的dealloc方法
This is how I would write the snippet with viewAccountsretaining
这就是我将如何在保留viewAccounts 的情况下编写代码段
if(!self.viewAccounts)
{
ViewAccounts *objViewAccounts= [[ViewAccounts alloc] initWithFrame:CGRectMake(0, yViews, 320, 400) withViewController:self];
[self.view addSubview:objViewAccounts];
self.viewAccounts = objViewAccounts;
[objViewAccounts release];
}
...
- (void) dealloc {
[super dealloc];
[viewAccounts release];
...
}
What happened in your original code was that the retaining variable viewAccountswas not released properly at the end of the this class life cycle and so dealloc was not called.
在您的原始代码中发生的情况是保留变量viewAccounts在此类生命周期结束时未正确释放,因此未调用 dealloc。
BTW when you only assignthe variable then you have to manage the retain yourself if you re-assignthat variable especially if you NIL it. Assume that you set the variable to NIL then the snippet could be executed again. But then you overwrite the variable and you are not ableto release the first value. If you use retainthe Object-C will do that for you. As a rule of thumb I would always use retainfor properties until you have enough experience to handle the other cases.
顺便说一句,当您只分配变量时,如果您重新分配该变量,则必须自己管理保留,特别是如果您将其设为NIL。假设您将变量设置为 NIL,则可以再次执行该代码段。但是随后您覆盖了该变量并且您无法释放第一个值。如果您使用保留,Object-C 将为您做到这一点。根据经验,我将始终对属性使用保留,直到您有足够的经验来处理其他情况。
回答by Oscar Gomez
You could set your objViewAccounts to autorelease, since it will be retained by self.view anyway.
您可以将 objViewAccounts 设置为 autorelease,因为它无论如何都会被 self.view 保留。
ViewAccounts *objViewAccounts= [[[ViewAccounts alloc] initWithFrame:CGRectMake(0, yViews, 320, 400) withViewController:self] autorelease];
Also regarding the retain count, if the property self.viewAccounts is declared as retain, that would also increase the retain count by 1.
同样关于保留计数,如果属性 self.viewAccounts 被声明为保留,那么保留计数也会增加 1。