user737645
user737645

Reputation:

How to solve this memory leak?

+ (UITableViewCell *)inputCell {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;  
    cell.textLabel.font = [UIFont boldSystemFontOfSize:14];
    return cell;
}

Xcode is showing a memory leak. I tried giving auto release during cell initialization and during return, but the app crashed on both occasions.

Upvotes: 3

Views: 286

Answers (2)

visakh7
visakh7

Reputation: 26400

UITableViewCell *cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"id"] autorelease];

should be fine unless you have a problem with the code from where you are calling -inputCell

Upvotes: 0

RedBlueThing
RedBlueThing

Reputation: 42542

The code you have posted will leak memory because your alloc init will return a cell with a retain count of one. Presumably the calling code is then returning this object to a cellForRowAtIndexPath which will attach it to the UITableView and increment the retain count again (to two). So when the UITableView releases it's memory, the object will still have a retain count of one.

If you tried autoreleasing the object in this code and it crashes, then you have a separate bug.

Upvotes: 1

Related Questions