UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellId];
if (cell == nil)
{
[[NSBundle mainBundle] loadNibNamed:@"ThemeCell" owner:self options:nil];
cell = self.themeCell;
self.themeCell = nil;
}
...
return cell;
My understanding is that self.themeCell = nil; should destroy the object since there is no longer any owner of it. cell = self.themeCell doesn’t retain it, but just assigns it. So what keeps the cell alive? I can only assume that the synthesized property is autoreleasing the old value instead of releasing it immediately. Is this the case?
The nib loading process is slightly (but not very) complicated, and differs between the OSX and iPhone platforms. You can read about this in the Nib Object Life Cycle section of the Resource Programming Guide. In table 1-1 you will find this:
So what happens is that the cell is created with a retain count of 1, and then when it is set with your synthesized setter, that it increased to 2. When you set your property to
nilthe retain count goes down to 1, and the cell is returned to the table view. The table view adds it to its view hierarchy, and thereby retains it (and maybe retains it in other parts of its logic as well). After all this, theautoreleasepool is drained.No, synthesized setters release the object immediately. (Although some framework classes might hold on to the object a bit longer, if it’s a view that needs to be animated out for example.)