iPhone - 包括子视图和可能的内存泄漏?
在编写此常见语句时:
UIViewController* viewController = [[UIViewController alloc] initWithNibName:@"myXIB" bundle:nil];
/* do some things with the viewController */
[self.view addSubview:viewController.view];
我想知道这里是否可能存在内存泄漏,因为:
- 代码不会释放 viewController ,
- 在将 viewController 插入到 self.view 中后,调用 [viewController release] 的
- 从而导致应用程序崩溃viewController 不被自己所知.view 因为它不是直接插入的,所以释放 self.view 不应该释放 viewController
所以我在这里看到内存泄漏,viewController 永远不会被释放。
您能给我您的专业知识吗?
注意:Instruments 没有发现任何内存泄漏。
When writing this common statements :
UIViewController* viewController = [[UIViewController alloc] initWithNibName:@"myXIB" bundle:nil];
/* do some things with the viewController */
[self.view addSubview:viewController.view];
I'm wondering if there is a possible memory leak here, because :
- viewController is not released by code
- calling [viewController release] after having inserted it in self.view makes the app crash
- viewController is not known by self.view because it is not directly inserted, so releasing self.view should not release viewController
So I see a memory leak here where viewController is never released.
Could you give me your expert knowledge about that ?
Note : Instruments does not find any memory leak there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您希望视图作为子视图,我认为最好将视图控制器作为类的实例变量,并在
dealloc
UPDATE 中释放它。取自
之前的SO问题
If you want view as a sub view I think its only better to have the view controller as an instance variable of the class and
release
it indealloc
UPDATE
Taken from a previous SO question
是的,内存会泄漏,你应该总是释放自己分配的内存。
当您添加
[viewController release]
时,viewController会立即释放,然后您的应用程序崩溃,因为它正在尝试访问已释放的内存。为了防止这种情况,您应该将 viewController 作为类的实例变量包含在内。Yes, memory will leak, you should always release memory allocated by yourself.
When you add
[viewController release]
the viewController is dealloced immediately and then your app crashes, because it's trying to access dealloced memory. To prevent it, you should include your viewController as instance variable of your class.当您分配一个新的 UIViewController 但从未释放它时,这里肯定存在泄漏(当您进行分配时,它不会添加到自动释放池中)。您必须保留对此 viewController 对象的引用并稍后释放它。
正如您所说,如果您在 [viewController release] 之后立即调用,它确实会使您的应用程序崩溃,因为您刚刚将其内部视图添加到 self.view 但该视图(viewController)所属的控制器的引用计数器尚未增加。
There is definitely a leak here as you allocate a new UIViewController but never release it (it is not added to the autorelease pool as you do an allocation). You have to keep a reference on this viewController object and release it later on.
As you said, if you call right after [viewController release], it does crash your application because you have just added its internal view to self.view but the reference counter of the controller to which this view (viewController) belongs has not been incremented.
Tableview didselrowatindex 方法是发生此类内存泄漏的常见地方。
消除内存泄漏的一种方法是确保只创建 ViewController 的一个实例,然后重用它。
此外,这种技术不仅适用于创建和显示视图控制器,还适用于任何事物。如果您在代码中创建任何内容(例如 uilabel 等),请确保您没有创建多个实例。
/* 代码 */
/// 然后在你的代码中,tableview didSelectRow 等
Tableview didselrowatindex method is a common place where such memory leaks occur.
One way to do away with memory leaks is to make sure you only create one instance of the ViewController and then reuse it.
Furthermore this technique doesn't just apply to creating and showing viewcontrollers but anything. If you are creating anything in code such as uilabels, etc, make sure that you are not creating multiple instances.
/* code */
/// then later in your code , tableview didSelectRow, etc