Zhen
Zhen

Reputation: 12431

Objective C: How to resolve Leak in Code (results from Instrument)

I ran the instruments for my app (which contains a UITableView) and got the following results

The cell will will call the method [UICustomButton SetButtonWithAnswer....] everytime the cell becomes visible

EDIT: Added more screenshots

enter image description here

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

The issue is that I am not sure what exactly is causing the leak. i have released all my alloc inits in the code. Why is it still leaking?

Any advise on this will be greatly appreciated!

EDIT:

I added the UICustom Buttons as follows

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"];

Upvotes: 3

Views: 234

Answers (3)

bshirley
bshirley

Reputation: 8357

Every alloc/init inside setButton:… needs to be rethought. You do not want to recreate those views just to set the values.

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

self.imageView.image = self.image;

and the same for the label

Upvotes: 2

Tomasz Stanczak
Tomasz Stanczak

Reputation: 13164

If I understand your code right you want a single copy of your custom button for each of "like", "comment" and "join in"? Then I think that you are adding too many custom buttons: they are getting added if cell=nil. UITableView creates a cell for each visible row, so there will be as many copies of each of them as visible rows.

Did you actually check if the cells are reused properly? That is that there have been only as many created as visible rows?

Next what about getters of self: answerForCell, navcon and answer.likers: are there any opened retains?

Upvotes: 2

drekka
drekka

Reputation: 21883

it may be further down, but I cannot see tempLabel being released. You really need to tell us what is leaking. You should be able to work out whether it is the ImageView, label, etc.

Upvotes: 1

Related Questions