Question

This is the code of my cellForRowAtIndexPath of my UITableView

    - (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;
}

This is leaking like hell. Every time I scroll the table, more leaks are added to the list on instruments.

Can you guys spot why?

thanks for any help.

EDIT

After the first round of comments, I have changed the previous code to this

- (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;
}

leaks continue. No change at all.

Was it helpful?

Solution

Looking at http://imgur.com/XPRYF this does not look like it's your leak. Is this on the device or in the simulator?

There used to be leaks of GSEvents when you used the accelerometer but those should have been fixed in 3.0.

Also try disabling NSAutoreleaseFreedObjectCheckEnabled, NSZombieEnabled and NSDebugEnabled if anyone of those are enabled since that has tripped up the profiler for me a number of times.

The one other thing I'm curious about is why you set up you cells differently depending on if they come off the reusequeue or not but that's a separate issue.

OTHER TIPS

I would avoid autorelease here, as you're probably going to be creating objects many times before the autorelease pool gets a chance to drain. Also, it's not so great in performance intense situations (like scrolling a table).

edit

I should add, in case there's confusion, instead of using autorelease here, you would instead do

myThing = [[Obj alloc] initWithWhatever];
[myThing doStuff];
[cell addSubview:myThing];
[myThing release];

Have you verified that identificadorNormal and @"normal" are the same? If you keep trying to dequeue a cell with identifier @"normal", but none exist, then you're going to create a cell with identifier identificadorNormal. If that's not the same as @"normal", then you're going to keep creating new cells every time the tableview tries to display a cell.

I wonder if the leaks happen when UIImage loads the images?

Can you try using +imageNamed instead? UIImage will cache the images then (it doesn't with +imageWithContentsOfFile:). If the leaks happen while loading images, that should reduce the leakage quite a bit, but proably wouldn't eliminate it.

NSString *imageName = [NSString stringWithFormat: @"table%d.jpg", indexPath.row];
cell.imageView.image = [UIImage imageNamed:imageName];         

I replicated your code in my own project, and it leaks in the simulator, but doesn't leak on either of the devices I have (iPhone 3GS, iPod Touch 2G).

Assuming you're seeing trouble in the simulator too, I assume this is just another confirmation of Apple's recommendation that you test on a real device.

Never posted here before and not sure if I'm following the topic correctly. My suggestion would be to check the compiler optimization level you are using. With certain projects (I can detail later if this approach helps) funky problems like this one go away by using -o or -o1. Anything higher and it seems like the initialized values of instance variables can't always be trusted.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top