1

I have property:

@property(nonatomic, retain) NSMutableArray *myvar;

First case:

    myvar = [[NSMutableArray alloc] init];
    NSLog(@retainCount: %i:", [myvar  retainCount]);

outputs:

retainCount: 1

Second case:

    self.myvar = [[NSMutableArray alloc] init];
    NSLog(@retainCount: %i:", [self.myvar  retainCount]);

outputs:

retainCount: 2

My question is: why in the second case retain value is 2 ?

vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
przemeko
  • 39
  • 3
  • possible duplicate of [Why retain count in negative value?](http://stackoverflow.com/questions/14601270/why-retain-count-in-negative-value) and [this](http://stackoverflow.com/questions/12418230/why-is-the-retain-count-of-1-equal-to-7-8-or-10), and [this one too](http://stackoverflow.com/questions/12883616/ios-reference-counting), and [this one as well](http://stackoverflow.com/questions/14043994/ios-memory-management-clarifications). –  Feb 16 '13 at 16:51
  • There are many questions here about why not to use retain count for your memory management. Here's a a link that explains it too. http://www.friday.com/bbum/2011/12/18/retaincount-is-useless/ – nevan king Feb 16 '13 at 16:57

4 Answers4

8

The only valid answer: never check, use, trust retainCount. it is not meant for debugging or use directly for memory management.

useful: whentouseretaincount.com

But in your case: As you are not using ARC (otherwise the compile wouldnt allow you to use retainCount anyway) you are over retaining.

it retain here @property(nonatomic, retain) NSMutableArray *myvar;

and here: self.myvar = [[NSMutableArray alloc] init];

do:

self.myvar = [[[NSMutableArray alloc] init] autorelease]; //will release once soon

or my favorite, as independent from ARC/MRC and short

self.myvar = [NSMutableArray array]; // identical to the first example

or more explicit

NSMutableArray *var = [[NSMutableArray alloc] init]; 
self.myvar = var; 
[var release];
Zoe
  • 27,060
  • 21
  • 118
  • 148
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
4

+1 because you alloc/init'd it
+1 because self.myvar retains it (as set out in your property declaration)

if you autorelease after alloc/init, it will go back down to 1... then if you set self.myvar to nil, it will hit 0 (if nothing else has retained it in the meantime)

But as vikingosegundo has said, you don't want to be messing with retain counts. The OS determines when to knock them back down, so you can't use them as a reliable measure of state.

foundry
  • 31,615
  • 9
  • 90
  • 125
1

Here's a rule that I used to go by before ARC came along:

'The NARC rule':

If you use New, Alloc, Retain or Copy, increase the count by one.

Your retain count is 2 in the second case because you are using alloc and retain on the instance property of the class (by using the self keyword). In the other case you are not using the synthesized setter to set the variable, and therefore not using retain. Explore what properties actually do for increased understanding.

vikingosegundo has explained well the correct way to do things in his answer.

Hope that helps!

Adam Waite
  • 19,175
  • 22
  • 126
  • 148
0

See this answer: Question About Retain Counts & initializing member variables

First, you alloc init a new NSArray object. That's a retain count of 1. Second, your setter sends the object a retain message when assigning it to your instance var. That bumps the retain count up to 2.

Cheers.

Community
  • 1
  • 1
jlmendezbonini
  • 2,239
  • 21
  • 25