开发者

Potential leak of an object, Build and Analyze, viewForHeaderInSection

开发者 https://www.devze.com 2023-01-11 01:12 出处:网络
I\'ve just mad Build and Analyze with my project on iphone, and i got warning from analyzer which i don\'t understand, this is my function:

I've just mad Build and Analyze with my project on iphone, and i got warning from analyzer which i don't understand, this is my function:

- (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 = 开发者_StackOverflow中文版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__);     
}

and in the lines with return customView, the analyzer says: "potential leak of an object allocated in line ###", can somebody explain me why it's hapenning?


You need:

return  [customView autorelease];

You allocate your customView and don't release it anywhere in your code. Once you pass it to table view it will get retained so you can safely dispose of its ownership - by sending autorelease message.

P.S. you also forgot to release headerLabel in section == 6 branch

0

精彩评论

暂无评论...
验证码 换一张
取 消