To retain or not to retain, a retain gotcha!

I came across a little gotcha today cause by a habit of using @property (nonatomic, retain) for object properties.  The problem in this case was a UITableView controller that was refusing to be deallocated because its reference count would never reach 0.

I eventually tracked down the problem to my custom table view cell object which had the following property:

AppTableView *table;   // Subclassed UITableViewController
...
@property (nonatomic, retain) AppTableView *table;

and when using the table view cell I was doing

cell.table = self;  // reference back to the table

What this meant however was that because the cell had now retained the UITableViewController the controller could never be deallocated, because to release the cells required the controller to deallocate but the controller could not deallocate because the cells have not been released.  Catch 22.

The solution was to change the retain to assign and remove the [table release] from the cells dealloc method:

@property (nonatomic, assign) AppTableView *table;
    // Weak reference to table view

Advertisements

About austinfrance

Technical Developer @ RedSky IT / Explorer Software
This entry was posted in Development, iPhone. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s