views:

783

answers:

6

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.

+2  A: 

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];
jbrennan
Good idea, and certainly doesn't hurt, but I don't think that this would be a big problem here. In the first run loop (when the table is displayed the first time) this method should only be called like 15 times (however many rows fit on the screen), and those 15 UITableViewCells will be released at the end of that run loop. Later, while scrolling, -cellForRowAtIndexPath would only occasionally create a new UITableViewCell, because it will often be able to reuse existing cells.
Thomas Müller
Hi, thanks for your answer, but I see no difference at all. Same leaks and I am doing exactly what you've suggested.
Digital Robot
A: 

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.

Dave DeLong
thanks. I have corrected that, but incredibly the leak continues. :-(
Digital Robot
I have commented all lines creating the labels on the code and the leak vanished. The problem is there.
Digital Robot
A: 

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];
Thomas Müller
I have commented out the LABEL lines (creation and settings) and the leaks vanished. So the problems are in these lines.
Digital Robot
+2  A: 

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.

monowerker
I never use the simulator. I am compiling for 3.0 and seeing these leaks on the iPhone.
Digital Robot
I have commented out the LABEL lines (creation and settings) and the leaks vanished. So the problems are in these lines. THe leaks you see on the image comes from the label lines, not the images.
Digital Robot
I discovered that this was two bugs in one. The guilty was really the accelerometer. But the problem is that the accelerometer do not leaks alone. If I comment the labels, the accelerometer do not leaks. If I active the labels, the accelerometer makes them leak. How can that be??? The labels leak if the accelerometer is on, AND I AM COMPILING FOR 3.0 where, in theory, the accelerometer is fine.
Digital Robot
Could be that i remember wrong and the leak with the accelerometer got fixed in 3.1. I am not sure it's an acceptable fix for your application but you can try removing the accelerator delegate in touchesBegan and the restore it in touchesEnded. That should get rid of the leaks but you accelerometer delegate won't get called while there are touches on the screen then...
monowerker
A: 

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.

refulgentis
I never use the simulator. I am compiling for 3.0 and seeing these leaks on the iPhone.
Digital Robot
I have commented out the LABEL lines (creation and settings) and the leaks vanished. So the problems are in these lines.
Digital Robot
Bizarre. I saw your earlier comment about only the label lines causing trouble and only replicated those. I'm not seeing trouble on a 3GS and iPhone 2G running 3.2.1...very, very, bizarre.
refulgentis
have you created a table with more than 7 lines? I think you have to create at least 10 to 20 lines to see the problem. I am getting desperate with this. I remove the labels, no leak. I add the labels, leak. I don't know what more to do. I have to imagine a new way to populate these cells...
Digital Robot
Actually I did 10,000 for the heck of it. :-PIf you're going an alternate route, I suggest using Interface Builder.
refulgentis
A: 

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.

John
please elaborate your explanation...
Digital Robot