开发者

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 dur开发者_开发问答ing 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜