Reputation: 2941
I am getting a memory leak in Instruments related to the table view delegate method cellForRowAtIndexPath when using custom table view cells. I am using XCode 5 but ARC is disabled. I have created my custom table view cell as a separate xib and I load that xib in my viewDidLoad method using nibWithNibName (which, if i remember, checks whether you have a cell or not so you dont have to check if cell != nil in the delegate method). Below are the sections of code that are relevant:
static NSString *const TransactionResultCellIdentifier = @"TransactionResultCell";
- (void)viewDidLoad
{
[super viewDidLoad];
UINib *cellNib = [UINib nibWithNibName:TransactionResultCellIdentifier bundle:nil];
[self.transactionsTableView registerNib:cellNib forCellReuseIdentifier:TransactionResultCellIdentifier];
cellNib = [UINib nibWithNibName:LoadingCellIdentifier bundle:nil];
[self.transactionsTableView registerNib:cellNib forCellReuseIdentifier:LoadingCellIdentifier];
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
TransactionResultCell *cell = (TransactionResultCell *)[tableView dequeueReusableCellWithIdentifier:TransactionResultCellIdentifier];
if(self.isLoading){
return [tableView dequeueReusableCellWithIdentifier:LoadingCellIdentifier];
}
else{
TransactionResult *transactionResult = [self.transactionResults objectAtIndex:indexPath.row];
cell.transactionDescriptionLabel.text = [NSString stringWithFormat:@"%@: %@", transactionResult.transactionID, transactionResult.transactionDescription];
cell.transactionPriceLabel.text = [@"Price: $" stringByAppendingString:transactionResult.transactionPrice];
self.totalPrice += [transactionResult.transactionPrice doubleValue];
}
return cell;
}
Instruments points me to the line up above where i am attempting to dequeue the custom table view cell along with obvious UILabel leaks that are part of the xib structure that i custom built in IB.
Can anyone point me to a solution here? Thanks...
Upvotes: 0
Views: 467
Reputation: 1682
On viewWillDisappear:animated, call to nil out your nibs.
[self.tableView registerNib:nil forCellReuseIdentifier:[WIActivityNameLabelCell reuseIdentifier]];
I have 5 custom nibs in my view, and I was losing about 0.2mb per load, which added up. I suspect it is because I keep my nib and reuseIds in the actual custom table view cell, calling them as class methods. I think this was creating some kind of strong reference that was leaking. Anyway, nilling the nib solved it. Note that the apple documentation states:
If you previously registered a class or nib file with the same reuse identifier, the nib you specify in the nib parameter replaces the old entry. You may specify nil for nib if you want to unregister the nib from the specified reuse identifier.
Upvotes: 1
Reputation: 104082
The problem could be that first line in tableView:cellForRowAtIndexPath:
. You create a TransactionResultCell that you don't do anything with if self.isLoading evaluates to true. That line should be inside the else clause (as well as the return cell
line).
Upvotes: 2