简体   繁体   中英

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.

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM