【问题标题】:release object of a return method object c返回方法对象 c 的释放对象
【发布时间】:2012-06-02 22:15:45
【问题描述】:

在使用分析构建运行应用程序时,Xcode 发现我有很多内存泄漏,特别是有一个我不知道如何解决它:

- (UIView *) tableView:(UITableView *)tableView 
viewForHeaderInSection:(NSInteger)section {



UIImageView *sectionImage = [[UIImageView alloc] init];
if (section == 0)sectionImage.image = [UIImage imageNamed:@"myImage.png"];


return sectionImage;
}

所以我的问题是,我怎样才能发布这个sectionImage? if是方法的return?

编辑:

我还有一个问题,分析给我另一个内存泄漏,我有这个:

.h @property(非原子,保留)NSIndexPath *directCellPath;

.m

@synthesize directCellPath = _directCellPath;

- (id)init{
if ((self = [super initWithNibName:@"MyViewController" bundle:nil])) {


    self.directCellPath = [[NSIndexPath alloc] init];

}
return self;
}

然后在代码中我使用它,最后在 dealloc 中我这样做:

- (void)dealloc {

[_directCellPath release];

[super dealloc];
}

并在这一行给我一个内存泄漏:

self.directCellPath = [[NSIndexPath alloc] init];

如果我在 dealloc 中释放了它,为什么?

【问题讨论】:

    标签: iphone ios memory-leaks


    【解决方案1】:

    你必须像这样使用自动释放

    UIImageView *sectionImage = [[[UIImageView alloc] init] autorelease];

    【讨论】:

      【解决方案2】:

      为了回答你的第二个问题,你发布了_directCellPath,而不是directCellPath。有区别。

      【讨论】:

      • 但是如果我做 _directCellPath 不起作用,如果我做 self.directCellPath 告诉我我不再是所有者......
      【解决方案3】:

      你必须这样做

      @synthesize directCellPath 
      

      还有这个

       - (void)dealloc 
      {
      
      self.directCellPath = nil;
      
      [directCellPath release];
      [super dealloc];
      
      }
      

      【讨论】:

        猜你喜欢
        • 2010-11-18
        • 1970-01-01
        • 1970-01-01
        • 2011-06-23
        • 1970-01-01
        • 2011-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多