开发者

Who can help me find the memory allocations and leaks?

Who can help me find the memory allocations and leaks?

// 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];
开发者_StackOverflow
    //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;
}


  1. curItem has to be released if it has retain property

Other than this i cannot see any leaks in this piece of code. Leak may lie somewhere else.


Maybe I'm missing something but I can't see anything in there that would be technically considered a leak. The only problem I can see is that you are adding all your buttons every time the cell is drawn so you will end up with a lot of labels and buttons and may run out of memory. You can fix this by removing all the subviews each time. When doing this you may want to add your buttons to cell.contentView instead of just to the cell otherwise you may end up removing views that you don't mean to.


itemBtn is leaking. To figure out why it happens you should understand how UITableView works. When it dequeues cell from invisible at the moment cells, it adds itemBtn even if it was already added.

You should subclass UITableViewCell and add itemBtn on it in init method. In tableView:cellForRowAtIndexPath: you should only set some properties and do not add any subviews on your cell.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜