I am doing some research on reference count increase. Please help on finding it.
Below is sample code and research i’m doing what would happen of reference counting for each line below.
.h file
NSArray *tempArray;
@property (nonatomic, retain) NSArray *tempArray;
.m file
@synthesize tempArray;
-(void) sampleFunction
{
NSArray *myArray = [[NSArray alloc] init]; // Thinking reference count increases to "1"
tempArray = myArray;// reference count increases and tempArray gets retain count "1" now.
tempArray = myArray;// reference count increases and tempArray gets retain count "2" now.
tempArray = [NSArray arrayWithObject:@"SomeString"]; // retain count = ?
}
I know this code may not be for functioning, but this is for only researching about what will happen on reference counting for such scenarios. I tried printing retainCount, but it doesn’t show the correct result. Please advise me how does the reference count works on this each line?
In lines 2, 3 and 4 you are affecting the instance variable
tempArrayto the same object asmyArray. But if you write it this way, you try to affect an instance variable. As a matter of fact, if you didn’t write any@synthesize tempArrayor@synthesize tempArray = tempArrayin your code, by default the instance variable generated automatically to store the property value is the same name as the property name, but prefixed with an underscore. So as the property name istempArray, the instance variable is named_tempArray. The instance variabletempArrayitself does not exist and your line of code is invalid.So if we suppose you wrote instead:
self.tempArray = myArray(which is equivalent to[self setTempArray:myArray];and thus call the property setter), so you set the property to point to the same array you created in (1). This array is thus retained by the property, and its retainCount increses by one, because it is retained bymyArrayand by theself.tempArrayproperty.In (3) you affect the property to the very same object as before. This the ref count does not change at all. You could understand that as if you replaced the value of the
self.tempArraywith another value, so the setter of the property release the old value (decrementing its ref count), then retain the new value (thus incrementing its ref count). As in your case the old and new values are the same object, you would decrement the ref count of your array then re-increment it again. In practice, the ref count does not even change at all (instead of decrementing+incrementing again) to avoid any potential dealloc of the object, because the default implementation of a property setter is as follow:In (4) you replace again the value of the property
tempArray, but this time with a completely new object. So the property will release its old value and retain the new one. Thus the first array you created in (1) which had a refcount of 2 (retained bymyArrayand byself.tempArray) decrease its ref count to 1 (because the property won’t retain it anymore), and the new instance you created[NSArray arrayWithObject:@"SomeString"]is retained by the property, so its ref count is +1.If you replaced
self.tempArray = ...(so the use of the property) with the direct use of the instance variable, using instance variables don’t retain the objects they are affected to (except if you are using ARC but it seems you don’t), so the ref count of the object wouldn’t have changed at all in (2), (3) and (4).