Search code examples
objective-ccocoa-touchretaincount

NSObject retainCount not showing the proper value


I am having the property as follows

@property(strong,nonatomic)NSArray *dataArray;

I am trying to display the retainCount as follows

- (void)viewDidLoad    
{        
         [super viewDidLoad];
       // Do any additional setup after loading the view, typically from a nib.

          self.dataArray=[NSArray new];

         NSLog(@"Retain Count1 %d",[dataArray retainCount]);

        [dataArray release];

        NSLog(@"Retain Count2 %d",[dataArray retainCount]);        
}

As per as my understanding Retain Count1 and Retain Count2 should be 1 and 0 resp.

But I am getting the strange values 22 and 21 resp and when I run again sometimes the

retain count will be again incremented by 1 . Any help is greatly appreciated .


Solution

  • Do not ever use retainCount for tracking object's references as the object might be retained internally without you knowing it causing the retainCount to show what you think is an incorrect result.

    This site will help you further :)