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 shows a memory leak. I tried to provide auto-release during cell initialization and during return, but the application crashed in both cases.

+3
source share
2 answers

The code you posted will be a memory leak, because your init init will return the cell while maintaining the number of units. Presumably, the calling code returns this object to cellForRowAtIndexPath , which binds it to a UITableView and again increases the hold count (to two). Therefore, when a UITableView frees its memory, the object will still have a storage counter.

, , .

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

, , -inputCell

0

All Articles