【问题标题】:Memory allocation/deallocation of UIViewController.viewUIViewController.view 的内存分配/释放
【发布时间】:2011-06-19 13:31:26
【问题描述】:

我正在努力解决以下问题。

我正在调试一个似乎需要更多内存的应用程序。我将以下代码添加到“crashtest”视图控制器中:

NSLog(@"allocating 10000 instances of the MyViewController");
for (int i=0; i<10000; i++) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    MyViewController *aController = [[MyViewController alloc] initWithNibName:@"MyViewController" bundle:nil];
    if (aController.view == nil) {} 
    [aController release];
    [pool drain];
}
NSLog(@"done allocating 10000 instances of MyViewController");

当我在 Instruments/Allocations 中运行上述代码时,它的内存使用量All Allocations / live 在进入循环之前约为 5 Mb。循环运行后大约有 24 Mb。

如果我在禁用if (aController.view == nil) {} 行的情况下运行相同的代码,内存不会显着增加。

UIViewController 自动调用loadView(),因为我使用aController.view。所以我可以理解内存使用量的暂时增加。但是当我在控制器上调用release 时,不应该释放内存吗?或者,为视图分配的内存(并在 live bytes 下列出)实际上仅在内存不足的情况下才被释放?

【问题讨论】:

  • 听起来您的控制器/视图中可能存在泄漏。您是否忘记在-dealloc 中将一些 IBOutlets 设置为 nil?
  • 是的,它们都被计算在内。我有4个,每个在dealloc发布如下:[myButton release], myButton = nil
  • 如果“听起来可能有泄漏”,您应该检查它是否真的存在。使用 Instruments(有一个“Leaks”工具)运行您的测试程序可以告诉您是否真的有泄漏。
  • 仪器/泄漏并没有告诉我有泄漏。但这并不意味着没有,在某些情况下仪器不会将泄漏情况检测为泄漏。

标签: iphone cocoa-touch memory-management uiviewcontroller


【解决方案1】:

消息[viewController view] 加载视图(当/如果需要)。

【讨论】:

    猜你喜欢
    • 2011-12-09
    • 1970-01-01
    • 2011-05-13
    • 1970-01-01
    • 2010-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-01
    相关资源
    最近更新 更多