目标C:如何解决代码中的泄漏(仪器的结果)

时间:2011-07-04 06:00:07

标签: objective-c ios memory-leaks

我为我的应用程序(包含UITableView)运行了仪器并得到了以下结果

每当单元格变得可见时,单元格将调用方法[UICustomButton SetButtonWithAnswer ....]

编辑:添加了更多屏幕截图

enter image description here

enter image description here enter image description here enter image description here enter image description here

问题在于我不确定究竟是什么导致泄漏。我已经在代码中释放了所有的alloc inits。它为什么还在泄漏?

对此有任何建议将不胜感激!

编辑:

我添加了UICustom按钮如下

if (cell == nil)
{
    cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:PlaceholderCellIdentifier] autorelease];
    //Add like button
    UICustomButton *likeButton = [[UICustomButton alloc]init];
    likeButton.tag = 7;

    //Add comment button
    UICustomButton *commentButton = [[UICustomButton alloc]init];
    commentButton.tag = 8;

    //Add answer too button
    UICustomButton *answerButton = [[UICustomButton alloc]init];
    answerButton.tag = 9;

    [self.contentView addSubview:likeButton];
    [self.contentView addSubview:commentButton];
    [self.contentView addSubview:answerButton];

    [likeButton release];
    [commentButton release];
    [answerButton release];
}


//Set like button
UICustomButton *thisLikeButton = (UICustomButton *)[self.contentView viewWithTag:7];
[thisLikeButton setButtonWithAnswer:self.answerForCell buttonType:@"like" navcon:self.navcon andFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN, totalCommentLabelHeight + CELL_SPACING*4, 45, CELL_BUTTON_HEIGHT)];
thisLikeButton.imageView.image = [UIImage imageNamed:@"heart.png"];


//Set comment button
UICustomButton *thisCommentButton = (UICustomButton *)[self.contentView viewWithTag:8];
 [thisCommentButton setButtonWithAnswer:self.answerForCell buttonType:@"comment" navcon:self.navcon andFrame:CGRectMake(CELL_TEXT_LEFT_MARGIN + 45 + 5, totalCommentLabelHeight + CELL_SPACING*4, 80, CELL_BUTTON_HEIGHT)];
thisCommentButton.imageView.image = [UIImage imageNamed:@"chat.png"];

//Set answer button
UICustomButton *thisAnswerButton = (UICustomButton *)[self.contentView viewWithTag:9];    
[thisAnswerButton setButtonWithAnswer:self.answerForCell buttonType:@"join in" navcon:self.navcon andFrame:CGRectMake(1.5*CELL_TEXT_LEFT_MARGIN + 45 + 5 + 80 + 5, totalCommentLabelHeight + CELL_SPACING*4, 60, CELL_BUTTON_HEIGHT)];
thisAnswerButton.imageView.image = [UIImage imageNamed:@"beer-mug_white.png"];

3 个答案:

答案 0 :(得分:2)

如果我理解您的代码正确,您需要为“喜欢”,“评论”和“加入”中的每一个创建自定义按钮的单个副本吗?然后我认为你添加了太多自定义按钮:如果cell = nil,它们会被添加。 UITableView为每个可见行创建一个单元格,因此每个可见行的副本数量都会很多。

您是否确实检查过细胞是否正确使用?那就是创建的可见行只有很多?

接下来关于自我的吸引力:answerForCell,navcon和answer.likers:有没有开放的保留?

答案 1 :(得分:2)

setButton:…内的每个alloc / init都需要重新考虑。您只想重新设置这些视图以设置值。

if (self.imageView == nil) {
  UIImageView tempImageView = alloc/init …
  self.imageView = tempImageView;
  [tempImageView release];
}

self.imageView.image = self.image;

和标签相同

答案 2 :(得分:1)

它可能会进一步下降,但我看不到tempLabel被释放。你真的需要告诉我们泄漏的是什么。你应该能够弄清楚它是ImageView,标签等。