0

I thought it would be printed in order 0-1->0. This output was strangely different results. (0->3->2) Why is that?

ClassA.h :

@interface ClassA : NSObject
{
    NSArray *array;
}

@property(nonatomic, retain)NSArray *array;
@end

main.c

int main(int argc, const char * argv[])
{
    @autoreleasepool {
         ClassA *classA = [[ClassA alloc]init];
         NSLog(@"retainCount %lu", (unsigned long)[classA.array retainCount]);

         classA.array = [[NSArray alloc]init];
         NSLog(@"retainCount %lu", (unsigned long)[classA.array retainCount]);

         [classA.array release];
         NSLog(@"retainCount %lu", (unsigned long)[classA.array retainCount]);
    }
return 0;
}

Output :

2014-02-10 11:58:24.459 retainCount[4031:303] retainCount 0
2014-02-10 11:58:24.461 retainCount[4031:303] retainCount 3
2014-02-10 11:58:24.462 retainCount[4031:303] retainCount 2
ansible
  • 3,569
  • 2
  • 18
  • 29
realmasse
  • 523
  • 1
  • 6
  • 18
  • 9
    [Here's why you shouldn't trust `retainCount`.](http://stackoverflow.com/questions/5784084/calling-retaincount-considered-harmful?rq=1) – pasawaya Feb 10 '14 at 06:10
  • in addition to above link: refer to the golden rules: "you don't have to release which you have not retained" and "release a variable that many times you have retained it" – Saurabh Passolia Feb 10 '14 at 06:23
  • Also check :[why can i send messages to a deallocated instance of nsarray](http://stackoverflow.com/questions/13233853/why-can-i-send-messages-to-a-deallocated-instance-of-nsarray) – Midhun MP Feb 10 '14 at 06:31

0 Answers0