对象的潜在泄漏,Build and Analyze,viewForHeaderInSection

时间:2010-08-12 10:36:28

标签: iphone xcode uitableview

我刚刚在iphone上使用我的项目进行构建和分析,我收到了分析器的警告,我不明白,这是我的功能:

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {

 [self.tableView deselectRowAtIndexPath:indexPath animated:YES];

 NSLog(@"user did select row at index: %d.%d", [indexPath section], [indexPath row]);
}


- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
 NSLog(@">>> Entering %s <<<", __PRETTY_FUNCTION__);

 //UIView *customView;

 if(section ==6){


  UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 36.0)];

  UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  headerLabel.backgroundColor = [UIColor clearColor];
  headerLabel.opaque = NO;
  headerLabel.textColor = [UIColor whiteColor];
  headerLabel.highlightedTextColor = [UIColor whiteColor];
  headerLabel.shadowColor = [UIColor blackColor];
  headerLabel.shadowOffset = CGSizeMake(0, 1);
  headerLabel.font = [UIFont boldSystemFontOfSize:13];
  headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);
  headerLabel.text = @"Personal Data"; 
  [customView addSubview:headerLabel];

  return customView;

 }
 if(section ==10){


  UIView *customView = [[UIView alloc] initWithFrame:CGRectMake(10.0, 0.0, 300.0, 36.0)];

  UILabel * headerLabel = [[UILabel alloc] initWithFrame:CGRectZero];
  headerLabel.backgroundColor = [UIColor clearColor];
  headerLabel.opaque = NO;
  headerLabel.textColor = [UIColor whiteColor];
  headerLabel.highlightedTextColor = [UIColor whiteColor];
  headerLabel.shadowColor = [UIColor blackColor];
  headerLabel.shadowOffset = CGSizeMake(0, 1);
  headerLabel.font = [UIFont boldSystemFontOfSize:13];
  headerLabel.frame = CGRectMake(10.0, 0.0, 300.0, 30.0);
  headerLabel.text = @"Actions"; 
  [customView addSubview:headerLabel];
  [headerLabel release];

  return customView;

 }
 else {
     return nil;
   }

    NSLog(@"<<< Leaving %s >>>", __PRETTY_FUNCTION__);     
}

在返回customView的行中,分析器说: “在###中分配的对象的潜在泄漏”,有人可以解释为什么它会变成hapenning吗?

1 个答案:

答案 0 :(得分:1)

你需要:

return  [customView autorelease];

您分配了customView,并且不会在代码中的任何位置发布它。一旦将其传递给表视图,它将被保留,以便您可以安全地处置其所有权 - 通过发送自动释放消息。

P.S。你也忘了在section == 6分支

中发布headerLabel