Eyal
Eyal

Reputation: 10828

Objective c - Reference counting

Until five minutes I was sure that my understanding about Objective c reference counting is excellent, but when I started checking objects retainCount I was very surprised to see what I saw.

For example myViewController has a UITableview:

.h file

@interface RegularChatViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
{
     UITableView *_tableView;
}
@property (nonatomic, retain) IBOutlet UITableView *tableView; 

.m file

@synthesize tableView = _tableView;

- (void)loadView
{
    _tableView = [[UITableView alloc] init];  // STEP ONE
    NSLog(@"tableView retain count: %d",[_tableView retainCount]);

    self.tableView.frame = CGRectMake(0, 0, 320, tableHeight); // STEP TWO
    NSLog(@"tableView retain count: %d",[_tableView retainCount]);  

    [self.view addSubview:self.tableView]; // STEP THREE
    NSLog(@"tableView retain count: %d",[_tableView retainCount]); 
}

To my surprise the input was:

tableView retain count: 1
tableView retain count: 2
tableView retain count: 3

obviously STEP ONE increase retain count by 1 with alloc

I also know that STEP THREE increase retain count by 1 with addSubview

But whats going on in STEP TWO ??? why did it increase the retain count???
is there something to do with ARC??

Upvotes: 6

Views: 1514

Answers (5)

Deepak
Deepak

Reputation: 348

Well in step two :- by using self. tableView, getter of tableview property(which is set retain) will called. So as your property is allocated and retained both so retain count increases respectively.

Whenever you have to allocate a retained property , you should overwrite its getter method and this is called lazy instantiation.

Better allocate your tableview in its getter e.g.

-(UITableView *) tableView
{
     if(!_tableView) {
          _tableView  =  [[UITableView alloc]init];
     }

    return _tableView;
}

Upvotes: 0

Sulthan
Sulthan

Reputation: 130200

self.tableView.frame will retain and autorelease when returning the tableView from the getter.

Upvotes: 0

Stephen Darlington
Stephen Darlington

Reputation: 52575

I have a handy guide here: When to use retainCount?

In short, retainCount rarely means what you think it will. Without knowing how UITableView and UIView are implemented you can't know what the retain count should be. And we're not even taking autorelease into account...

Upvotes: 2

omz
omz

Reputation: 53561

As soon as you interact with any framework method or function whatsoever, the retainCount method becomes completely useless, because you don't know what these things do in their black boxes (they could add your objects to autorelease pools or whatever) and you shouldn't care about it.

Using retainCount to debug memory management issues is always a bad idea. See this answer for even more reasons to avoid it.

Upvotes: 2

graver
graver

Reputation: 15213

According to Apple docs on NSObject Protocol Reference for the retainCount method:

Important This method is typically of no value in debugging memory management issues. Because any number of framework objects may have retained an object in order to hold references to it, while at the same time autorelease pools may be holding any number of deferred releases on an object, it is very unlikely that you can get useful information from this method.

Upvotes: 7

Related Questions