开发者

Memory leaks in UITableView

I am using custom 开发者_运维知识库cells to populate one of my table. but whenever I scroll up and down, I can see there are some leaks happening, which points to UIKit Lib. Not sure why? For the reference I have attached a screen shot of the leaks from the Leaks Instrument.

Any help is appreciated!!

Memory leaks in UITableView

TableView cellForRowIndex:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    NSString *cellIdentifier = [NSString stringWithFormat:@"EditingCell_%d_%d",indexPath.section,indexPath.row];
    EditingTableViewCell *cell = (EditingTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];

    if (!cell) {
        [self.appCellNib instantiateWithOwner:self options:nil];        
        cell = editingTableViewCell;        
        self.editingTableViewCell = nil;
    }

Custom Cell:

@implementation EditingTableViewCell

@synthesize label,textField,commentField,dateLabel;

#pragma mark -
#pragma mark == Initialization == 

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // Initialization code.
    }
    return self;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];
}

#pragma mark -
#pragma mark == Memory Management ==

- (void)dealloc {
    [label release];
    [textField release];
    [commentField release];
    [dateLabel release];
    [super dealloc];
}


I'm not sure there is enough info to give a precise solution. However, my guess is that the properties are not well defined to be used in inflated xibs. For example you may have defined the property label as:

// Interface declaration
IBOutlet UILabel *label;

// property 

@property (nonatomic, retain) UILabel *label;

This is wrong and will cause leaks. The correct way for defining outlets is:

// Interface declaration
UILabel *label;

// property 
@property (nonatomic, retain) IBOutlet UILabel *label;

This makes it so that when the xib is inflated the setting of the variables goes through the synthesized setters thus the memory management works as you would expect.

Hope it helps! Cheers

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜