【问题标题】:Potential Memory leak, and I don't know why潜在的内存泄漏,我不知道为什么
【发布时间】:2012-01-01 08:51:12
【问题描述】:

我有以下代码将标签添加到 UITableView 的页脚中,以便我可以格式化文本(白色等)

它工作正常,但它给我一个“headerLabel”的泄漏警告,当它在“return”的行上分析它时

        // create the parent view that will hold header Label
UIView* customView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 15.0, 300.0, 44.0)];

    // create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:14];
headerLabel.textAlignment=UITextAlignmentCenter;
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 75.0);
headerLabel.numberOfLines=4;

if (section==0) {

    headerLabel.text = @"If turned off, the last used settings will be used on the next session\n\n"; // i.e. array element

}


[customView addSubview:headerLabel];

    //[headerLabel release];

return customView;
    // [customView release];

我试着把版本放在这里和那里,但它总是一样的。

非常感谢你们的反馈。

【问题讨论】:

  • 不,我不是。在 Xcode 4.2 上工作,但使用的是旧项目,所以我没有“现代化”它
  • 这个在SO上已经讲了很多次了,我觉得最好不要重复,搜索Cocoa内存管理。
  • 嗯,这是我第一次有潜在的内存泄漏,我检查了所有关于内存管理的文档,但我只是大脑受阻。很抱歉。

标签: iphone cocoa-touch debugging memory-leaks memory-management


【解决方案1】:

自动释放您的 customView 并确保在将 headerLabel 添加为子视图后释放它。每当您调用 alloc/init 时,您就获得了所有权,您需要确保释放这些对象。由于您要从此方法返回 customView,因此推迟释放该对象(使用自动释放)是有意义的,以便调用对象可以使用它。

// create the parent view that will hold header Label
UIView* customView = [[[UIView alloc] 
                          initWithFrame:CGRectMake(0.0, 15.0, 300.0, 44.0)] 
                          autorelease];

// create the button object
UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
headerLabel.backgroundColor = [UIColor clearColor];
headerLabel.opaque = NO;
headerLabel.textColor = [UIColor whiteColor];
headerLabel.highlightedTextColor = [UIColor whiteColor];
headerLabel.font = [UIFont systemFontOfSize:14];
headerLabel.textAlignment=UITextAlignmentCenter;
headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 75.0);
headerLabel.numberOfLines=4;

if (section==0) {
    headerLabel.text = @"If turned off, the last used settings will be used on the next session\n\n"; // i.e. array element
}

[customView addSubview:headerLabel];

[headerLabel release];

return customView;

【讨论】:

    【解决方案2】:
    1. 你必须在退出方法之前释放headerLabel

      [headerView release];
      
    2. 您可能应该自动释放customView,除非您的方法名称包含newalloccopy(在这种情况下,调用者必须释放返回的视图):

      return [customView autorelease];
      

    【讨论】:

      【解决方案3】:

      试试

      [headerLabel release];
      return [customView autorelease];
      

      【讨论】:

        【解决方案4】:

        根据您在此处获得的代码示例,第一个版本是正确的。 (在 return 语句之后释放没有意义)。您在创建对象时获得了该对象的所有权,您需要释放它。

        您可以使用 Instruments 来跟踪对象被保留和释放的位置;您可以查看泄漏对象的历史,以了解到底发生了什么。那将是您在这里的最佳选择。 使用 Leaks 工具启动您的应用程序,当您找到泄漏对象时,单击地址右侧的箭头。这将向您显示对象的历史记录 - 每次保留和释放。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-10-16
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-10-16
          相关资源
          最近更新 更多