views:

169

answers:

1

I'm still trying to find my way through memory management for the iPhone SDK, and I'm not sure why Instruments is reporting a certain block of code as a memory leak. I've followed tutorials for these parts of the code, so I'm not really sure what I'm doing wrong.

The violating block of code:

DreamTableCell *cell = (DreamTableCell *)[tableView dequeueReusableCellWithIdentifier:cellID];
if ( cell == nil )
    cell = [[[DreamTableCell alloc] initWithFrame:CGRectZero reuseIdentifier:cellID] autorelease];

Also, there is a custom method of DreamTableCell where the UITableViewCell's NIB file is loaded, nothing abnormal, as far as I know:

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    NSArray *objs = [[NSBundle mainBundle] loadNibNamed:@"DreamTableCell" owner:nil options:nil];
    for ( id item in objs )
        if ( [item isKindOfClass:[DreamTableCell class]] ) {
            self = item;
            break;
        }
    return self;
}

What's causing the memory leak here - what am I missing?

+4  A: 

It's the line:

self = item;

You're setting self to a new instance variable, and since you're in init, an existing instance has been created. You need to release the brand-new self before you set it to something else:

[self release];
self = item;
iKenndac
Agh, I don't know how I missed that :P Thanks.
hansengel