目标 C:如何解决代码泄漏(Instrument 的结果) [英] Objective C: How to resolve Leak in Code (results from Instrument)

查看:50
本文介绍了目标 C:如何解决代码泄漏(Instrument 的结果)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

每次单元格可见时,单元格都会调用方法 [UICustomButton SetButtonWithAnswer....]

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

添加了更多屏幕截图

问题是我不确定究竟是什么导致了泄漏.我已经在代码中释放了我所有的分配初始化.为什么还是漏水?

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!

我添加了 UICustom Buttons 如下

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

推荐答案

setButton:... 中的每个 alloc/init 都需要重新考虑.您不想只是为了设置值而重新创建这些视图.

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;

标签也一样

这篇关于目标 C:如何解决代码泄漏(Instrument 的结果)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆