代码之家  ›  专栏  ›  技术社区  ›  Duck

iphone:uiTableView像泰坦尼克号一样泄漏!

  •  3
  • Duck  · 技术社区  · 16 年前

    这是我的单元格代码,用于我的UITableView的rowatindexpath

        - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
            static NSString *identificadorNormal = @"Normal";
    
            UITableViewCell *cell;
    
            cell = [tableView dequeueReusableCellWithIdentifier:identificadorNormal];
    
            if (cell == nil) {
                cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                   reuseIdentifier:identificadorNormal] autorelease];
    
                UILabel * myText = [[[UILabel alloc] initWithFrame:CGRectMake(5.0, 54.0, 225, 18)] autorelease];
                [myText setTextAlignment:UITextAlignmentLeft];
                [myText setBackgroundColor:[UIColor whiteColor ]];
                [myText setClipsToBounds:YES];
                [myText setFont:[UIFont systemFontOfSize:14.0]];
                [myText setTextColor:[UIColor blackColor]];
                [myText setAlpha:0.6];
                [myText setTag: 1];
                [cell addSubview:myText];
    
                UILabel * labelFRE = [[[UILabel alloc] initWithFrame:CGRectMake(235.0, 54.0, 80, 18)] autorelease];
                [labelFRE setTextAlignment:UITextAlignmentCenter];
                [labelFRE setBackgroundColor:[UIColor greenColor ]];
                [labelFRE setClipsToBounds:YES];
                [labelFRE setFont:[UIFont boldSystemFontOfSize:14.0]];
                [labelFRE setTextColor:[UIColor blackColor]];
                [labelFRE setAlpha:0.75];
                [labelFRE setTag: 2];
                [cell addSubview:labelFRE];
            }
    
            cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
                pathForResource:[NSString stringWithFormat: @"table%d", indexPath.row] ofType:@"jpg"]];     
            NSString * preffix = [NSString stringWithFormat: @"grat%d", indexPath.row];
    
            UILabel *myText2 = (UILabel*)[cell viewWithTag:1];
            [myText2 setText:NSLocalizedString(preffix, @"")];
    
            UILabel *labelFRE2 = (UILabel*)[cell viewWithTag:2];
            [labelFRE2 setText:NSLocalizedString(@"frKey", @"")];       
            return cell;
    }
    

    这是地狱般的泄漏。 每次我滚动表时,更多的泄漏都会添加到仪器列表中。

    你们能找出原因吗?

    谢谢你的帮助。

    编辑

    在第一轮评论之后,我将以前的代码改为

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        static NSString *identificadorNormal = @"Normal";
    
        UITableViewCell *cell;
    
        cell = [tableView dequeueReusableCellWithIdentifier: identificadorNormal];
    
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier:identificadorNormal] autorelease];
    
            UILabel * myText = [[UILabel alloc] initWithFrame:CGRectMake(5.0, 54.0, 225, 18)];
            [myText setTextAlignment:UITextAlignmentLeft];
            [myText setBackgroundColor:[UIColor whiteColor ]];
            [myText setClipsToBounds:YES];
            [myText setFont:[UIFont systemFontOfSize:14.0]];
            [myText setTextColor:[UIColor blackColor]];
            [myText setAlpha:0.6];
            [myText setTag: 1];
            [cell addSubview:myText];
                [myText release];
    
            UILabel * labelFRE = [[UILabel alloc] initWithFrame:CGRectMake(235.0, 54.0, 80, 18)];
            [labelFRE setTextAlignment:UITextAlignmentCenter];
            [labelFRE setBackgroundColor:[UIColor greenColor ]];
            [labelFRE setClipsToBounds:YES];
            [labelFRE setFont:[UIFont boldSystemFontOfSize:14.0]];
            [labelFRE setTextColor:[UIColor blackColor]];
            [labelFRE setAlpha:0.75];
            [labelFRE setTag: 2];
            [cell addSubview:labelFRE];
                [labelFRE release];
        }
    
        cell.imageView.image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] 
            pathForResource:[NSString stringWithFormat: @"table%d", indexPath.row] ofType:@"jpg"]];     
        NSString * preffix = [NSString stringWithFormat: @"grat%d", indexPath.row];
    
        UILabel *myText2 = (UILabel*)[cell viewWithTag:1];
        [myText2 setText:NSLocalizedString(preffix, @"")];
    
        UILabel *labelFRE2 = (UILabel*)[cell viewWithTag:2];
        [labelFRE2 setText:NSLocalizedString(@"frKey", @"")];       
        return cell;
    }
    

    继续泄漏。完全没有变化。

    6 回复  |  直到 16 年前
        1
  •  2
  •   monowerker    16 年前

    看着 http://imgur.com/XPRYF 这看起来不像是你的泄密。这是在设备上还是在模拟器上?

    使用加速度计时,GSEvents曾出现泄漏,但应在3.0中修复。

    如果启用了其中任何一个,也可以尝试禁用nsautoreleasefreedobjectcheckenabled、nszombieenabled和nsdebugened,因为这会多次触发探查器。

    还有一件事我很好奇,那就是为什么你的手机设置不同,这取决于它们是否从续集中分离出来,但这是一个单独的问题。

        2
  •  2
  •   jbrennan    16 年前

    我会避免 autorelease 这里,因为在自动释放池有机会耗尽之前,您可能会多次创建对象。而且,在性能紧张的情况下(比如滚动一张桌子),它也不是很好。

    编辑

    我应该补充一下,以防混淆,而不是使用 自动复位 在这里,你会改为

    myThing = [[Obj alloc] initWithWhatever];
    [myThing doStuff];
    [cell addSubview:myThing];
    [myThing release];
    
        3
  •  0
  •   Dave DeLong    16 年前

    你证实了吗 identificadorNormal @"normal" 都一样吗?如果你一直试图用标识符将一个单元格出列 @“正常” ,但不存在,那么您将创建一个带有标识符的单元格 相同或正常 . 如果这和 @“正常” ,然后每次TableView尝试显示单元格时,您将继续创建新的单元格。

        4
  •  0
  •   Thomas Müller    16 年前

    我想知道当uiimage加载图像时是否发生泄漏?

    你能试试吗 +imageNamed 相反?uiimage将缓存图像(它没有 +imageWithContentsOfFile: )如果泄漏是在加载图像时发生的,那么应该会大大减少泄漏,但很可能不会消除泄漏。

    NSString *imageName = [NSString stringWithFormat: @"table%d.jpg", indexPath.row];
    cell.imageView.image = [UIImage imageNamed:imageName];         
    
        5
  •  0
  •   refulgentis    16 年前

    我在自己的项目中复制了您的代码,它在模拟器中泄漏,但在我拥有的任何设备(iPhone3GS、iPod touch 2G)上都不会泄漏。

    假设你在模拟器中也看到了问题,我假设这只是苹果建议你在真实设备上测试的另一个确认。

        6
  •  0
  •   John    16 年前

    以前从未在这里发布过,也不确定我是否正确地跟踪了主题。我的建议是检查您使用的编译器优化级别。对于某些项目(如果这种方法有帮助的话,我可以稍后详述),像这样的棘手问题可以通过使用-o或-o1来解决。任何更高的值,似乎实例变量的初始化值不能总是可信的。