【问题标题】:Who can help me find the memory allocations and leaks?谁能帮我找到内存分配和泄漏?
【发布时间】:2011-08-17 11:38:40
【问题描述】:

谁能帮我找出内存分配和泄漏?

// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{       
    NSInteger rowIndex = [indexPath row];//row index    
    static NSString *NineCellIdentifier = @"NineCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NineCellIdentifier];   
    if (cell == nil) 
    {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NineCellIdentifier] autorelease];
    }

NSNumber* aHeight = [heightArray objectAtIndex:rowIndex];
itemHeight = [aHeight integerValue];

NSInteger index = 0;
for (int j=0; j< columnNum; j++) 
{
    CGFloat x, y, w, h;
    x = 0;
    y = 0;
    w = itemWidth;
    h = itemHeight;

    index = rowIndex*columnNum + j;
    if (index >= [itemList count]) 
    {
        break;
    }
    //NSLog(@"index = %d, j= %d", index, j);
    curItem = [itemList objectAtIndex:index];
    NSString *imageFile = [appDelegate.clientResourcesDir stringByAppendingPathComponent:curItem.pic];
    UIImage *image = [UIImage imageWithContentsOfFile:imageFile];
    NSInteger imageWidth = image.size.width;
    NSInteger imageHeight = image.size.height;
    if (imageWidth > itemWidth) 
    {
        imageHeight = imageHeight*itemWidth/imageWidth;
        imageWidth = itemWidth;
    }       

    UIButton *itemBtn = [[UIButton alloc] initWithFrame:CGRectMake(x, y, w, h)];
    [itemBtn setBackgroundColor:[UIColor clearColor]];
    [itemBtn setTag:index];
    [itemBtn addTarget:self action:@selector(goNextPageView:) forControlEvents:UIControlEventTouchUpInside];

    if ([curItem.text1 isEqualToString:@""])
    {
        x = itemBtn.frame.origin.x + (itemWidth - imageWidth)/2;
        y = itemBtn.frame.origin.y + (itemHeight - imageHeight)/2;
    }
    else
    {
        x = itemBtn.frame.origin.x + (itemWidth - imageWidth)/2;
        y = itemBtn.frame.origin.y + (itemHeight - imageHeight - textSize - blankSize)/2;
    }       

    //1 button
    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(x, y, imageWidth, imageHeight)];
    [button setBackgroundImage:image forState:UIControlStateNormal];
    [button setTag:index];
    [button addTarget:self action:@selector(goNextPageView:) forControlEvents:UIControlEventTouchUpInside];
    [itemBtn addSubview:button];
    [button release];

    //2 label
    x = 0;
    y = y + imageHeight + blankSize; 
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, itemWidth, textSize)];
    label.font = [UIFont boldSystemFontOfSize:textSize];
    [label setTextAlignment:UITextAlignmentCenter];
    [label setBackgroundColor:[UIColor clearColor]];        
    label.textColor = [UIColor colorWithRed:rf green:gf blue:bf alpha:1];
    label.lineBreakMode = UILineBreakModeWordWrap;
    label.text = curItem.text1;
    label.tag = -1;     
    [itemBtn addSubview:label];
    [label release];

    //itemBtn.contentMode = UIViewContentModeBottom;
    CGRect frame = itemBtn.frame;
    frame.origin.x = j * itemWidth;
    frame.origin.y = 0;
    itemBtn.frame = frame;

    [cell addSubview:itemBtn];      
    [itemBtn release];
}
return cell;
}

【问题讨论】:

    标签: iphone uitableview memory-leaks memory-management


    【解决方案1】:
    1. 如果curItem有retain属性,则必须释放它

    除此之外,我在这段代码中看不到任何泄漏。泄漏可能在其他地方。

    【讨论】:

      【解决方案2】:

      也许我遗漏了一些东西,但我看不到任何在技术上被认为是泄漏的东西。我能看到的唯一问题是每次绘制单元格时都会添加所有按钮,因此最终会得到很多标签和按钮,并且可能会耗尽内存。您可以通过每次删除所有子视图来解决此问题。执行此操作时,您可能希望将按钮添加到 cell.contentView 而不是仅添加到单元格,否则您最终可能会删除您不想要的视图。

      【讨论】:

        【解决方案3】:

        itemBtn 正在泄漏。要弄清楚它发生的原因,您应该了解 UITableView 的工作原理。当它从此刻不可见的单元格中取出单元格时,它会添加 itemBtn,即使它已经添加了。

        你应该继承 UITableViewCell 并在 init 方法中添加 itemBtn。在 tableView:cellForRowAtIndexPath: 你应该只设置一些属性,不要在你的单元格上添加任何子视图。

        【讨论】:

          猜你喜欢
          • 2011-02-25
          • 1970-01-01
          • 2023-01-11
          • 2011-02-18
          • 1970-01-01
          • 2016-09-12
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多