Here is the following program about increasing d retain count value of a NSMutableArray type variable in interface part.
@property (nonatomic, retain) NSMutableArray *dataArray;
And implementation Part
NSLog(@"%d",[self.dataArray retainCount]);
self.dataArray = [[NSMutableArray alloc] init];
NSLog(@"%d",[self.dataArray retainCount]);
Now on first line its showing retain count value is O inside NSLog, however when we allocate the array it's increasing retain count to 2. I am not getting this thing clear, that why retain count is increased to 2 instead of 1. Please help me understanding this.
This might possibly help:
http://whentouseretaincount.com
In short you should never rely on the value returned by retain count.
You should use _object only if you are overriding getter/setter methods. "_object =" just assigns while "self.object =" calls its setter method. You must use _object to prevent and managing retain count using setter methods. Check the difference between by writing code:
@property (nonatomic, retain) NSMutableArray *arr;
//=============Accessing with _object==========
NSLog(@"Retain Count before Allocation => %lu", (unsigned long)_arr.retainCount);
_arr = [NSMutableArray arrayWithArray:@[@"arrobj1", @"arrObj3"]];
NSLog(@"Retain Count before Allocation => %lu", (unsigned long)_arr.retainCount);
_arr = [[NSMutableArray alloc] init];
NSLog(@"Retain Count After alloc+init => %lu", (unsigned long)_arr.retainCount);
//============================================
Output is :
Retain Count before Allocation => 0
Retain Count before Allocation => 1
Retain Count After alloc+init => 1
###### And also with accessing objects with self.object#######
//=============Accessing with _object==========
NSLog(@"Retain Count before Allocation => %lu", (unsigned long)self.arr.retainCount);
self.arr = [NSMutableArray arrayWithArray:@[@"arrobj1", @"arrObj3"]];
NSLog(@"Retain Count before Allocation => %lu", (unsigned long)self.arr.retainCount);
self.arr = [[NSMutableArray alloc] init];
NSLog(@"Retain Count After alloc+init => %lu", (unsigned long)self.arr.retainCount);
//============================================
Output is:
Retain Count before Allocation => 0
Retain Count before Allocation => 2
Retain Count After alloc+init => 2
I hope this will helps you.
The alloc init creates a retained instance +1. Then the retained property adds a retain on the assignment +2. To get a retain count of one, a) use ARC and a strong property, it will insert a release for you b) create an unretained instance:
self.dataArray = [NSMutableArray array];
or c) autorelease explicitly:
self.dataArray = [[[NSMutableArray alloc] init] autorelease];
Since the setter of the property is also getting called
self.dataArray = [[NSMutableArray alloc] init];
The retain count is getting pushed up to 2.
you can try this as well
NSMutableArray *temp=[[NSMutableArray alloc]init];
self.dataArray = temp;
[temp release];
The release called on temp balances out the extra retain.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.