views:

31

answers:

2
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?

A: 

AFAIK, synthesized properties use release, not autorelease.

It may be that the cell has a delegate or such specified in the NIB which is retaining ownership, or something in the loadNibNamed:owner:options: call sets an autorelease on the object that is still pending. Put NSLog(@"Retain Count: %d", [cell retainCount]); before and after the self.themeCell=nil; to verify.

John Franklin
+1  A: 

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:

Objects in the nib file are created with a retain count of 1 and then autoreleased. As it rebuilds the object hierarchy, however, UIKit reestablishes connections between the objects using the setValue:forKey: method, which uses the available setter method or retains the object by default if no setter method is available

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 nil the 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, the autorelease pool is drained.

I can only assume that syntesized property is autoreleasing the old value instead of release, is this the case?

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.)

Felixyz