UIView 未正确释放
我有一个很大的视图/视图控制器层次结构。
在主控制器中,我有以下代码,其中 aViewController 是 MyClass 的成员:
@implementation MyClass
...
- (void) viewDidLoad
{
[self.view addSubview:aViewController_.view];
[aViewController_ setDataSource:self];
[aViewController_ setDelegate:self];
}
- (void)dealloc
{
//[aViewController_.view removeFromSuperview]; // All ok when this is added
[aViewController_ release];
[super dealloc];
}
...
@end
运行此代码时,我看到 aViewController
从未被释放 - 保留计数最后仍为 1。
如果我添加 [aViewController_.view removeFromSuperview];
来解除分配,一切正常。
这是为什么呢? [super dealloc]
不应该负责视图的释放吗?视图在控制器之后被释放有什么关系吗?
我尝试用一个简单的测试应用程序进行重现,但没有任何运气。
I have a large hierarchy of view/viewcontrollers.
In the main controller I have the following code where aViewController is a member of MyClass:
@implementation MyClass
...
- (void) viewDidLoad
{
[self.view addSubview:aViewController_.view];
[aViewController_ setDataSource:self];
[aViewController_ setDelegate:self];
}
- (void)dealloc
{
//[aViewController_.view removeFromSuperview]; // All ok when this is added
[aViewController_ release];
[super dealloc];
}
...
@end
When running this, I see that aViewController
is never released - retain count remains 1 at the end.
If howeevr I add [aViewController_.view removeFromSuperview];
to dealloc the everything works fine.
Why is this? Shouldn't [super dealloc]
take care of the release of the view? Does it matter that the view is being released after the controller?
I have tried to reproduce with a simple test application without any luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这会将您的视图添加为 aViewController_.view 的子视图。
aViewController_.view
保留视图,因此当您释放主控制器时,视图不会被释放,这是正确的。在释放“主控制器”之前,您需要从超级视图中删除该视图。
当你释放它时,“主控制器”将会消失,但超级视图仍然保留子视图。
当您完成视图后,在释放拥有该视图的控制器之前,您可以在
aViewController_
中的某个位置执行此操作。如果没有看到你的所有代码,很难说。
请记住,当您将视图添加到另一个视图时,超级视图将保留该视图,直到将其删除。
This is adding your view to be a subview of aViewController_.view.
aViewController_.view
is retaining the view so when you release your main controller the view does not get deallocated that is correct.You need to remove the view from the superview before you deallocate your "main controller".
The "main controller" will be gone when you release it but the super view still has retention of the subview.
You can do this somewhere in the
aViewController_
when your finished with the view before you release the controller that owns the view.Its hard to say without seeing all your code.
Just remember that when you add the view to another view that the super view retains the view until it is removed.