Потенциальная утечка объекта, Build and Analyze, viewForHeaderInSection - PullRequest
0 голосов
/ 12 августа 2010

Я просто сошел с ума Build and Analyze с моим проектом на 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, анализатор говорит: «потенциальная утечка объекта, выделенного в строке ###», может кто-нибудь объяснить мне, почему это происходит?

1 Ответ

1 голос
/ 12 августа 2010

Вам необходимо:

return  [customView autorelease];

Вы выделяете свой customView и не выпускаете его где-либо в своем коде.Как только вы передадите его в табличное представление, оно будет сохранено, чтобы вы могли безопасно распоряжаться его владельцем - отправив сообщение об автоматическом выпуске.

PS Вы также забыли освободить headerLabel в section == 6 branch

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...