我应该释放 self.view 吗?
我有一个关于 UIViewController 中 self.view 的问题。
首先,在我的应用程序中,一切都是以编程方式创建的。通常我在loadView方法中创建self.view:
self.view = [[UIView alloc]initWithFrame:SCREEN_FRAME]autorelease]; // SCREEN_FRAME is a constant
此时,self.view的保留计数为1。
所以,我的问题是,当我使用完视图控制器时是否必须释放self.view?如果是这样,我应该在哪里释放它?
预先非常感谢:)
I have a question regarding to self.view in a UIViewController.
First, in my app, everything is created programmatically. And normally I create self.view in the loadView method:
self.view = [[UIView alloc]initWithFrame:SCREEN_FRAME]autorelease]; // SCREEN_FRAME is a constant
At this moment, the retain count of self.view is 1.
So, my question is, do I have to release self.view when I'm done with the view controller? If so, where should I release it?
Thanks very much in advance :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是通过 UIViewController 的实现为您完成的,只需确保在 dealloc 方法中调用
[super dealloc]
即可。That is being done for you by the implementation of UIViewController, just make sure you call
[super dealloc]
in your dealloc method.self.view添加了自动释放池,当池被释放时,对象也会被释放。你不需要明确地释放。
如果您将对象添加到池中并手动释放,您将得到双重释放异常(因为对象被释放两次)
self.view is added autorelease pool and object will be release when pool is released. you don't need to release explicitly.
If you add object to pool and release manually you will get exception double dealloc (since object is getting released twice)
仅供阅读本文的人参考,上述答案并不完全正确。 self.view 被称为 alloc、retain 和 autorelease,总保留计数为 1。retain 调用来自retain 属性和点语法。
因此它确实需要被释放,但正如 willcodejavaforfood 指出的那样,dealloc 的超级实现做到了这一点。
谢谢
Just for the reference of whoever might read this, the above answer is not exactly correct. self.view is called alloc, retain and autorelease, for the total retain count of 1. The retain call comes from the retain property and the dot syntax.
Therefore it DOES need to be released, but as willcodejavaforfood points out, the super implementation of dealloc does that.
Thanks