why is the retain count showing a value 2 in the below code?

422 views Asked by At
NSLog(@"retain count 1 for show detail -- %d",[showDetail retainCount]);

ChecklistDetail *detail = [appDelegates.arrayForChecklistDetails objectAtIndex:[sender tag]];

self.showDetail = detail;

NSLog(@"retain count 2 for show detail  -- %d",[showDetail retainCount]);

Here, in the above code, output of the first nslog is "retain count 1 for show detail -- 0", which is correct. However, the output of the second nslog comes as following "retain count 2 for show detail -- 2".

How does its retain count go upto 2?

help me if u can....

3

There are 3 answers

0
AudioBubble On BEST ANSWER

to check the Retain counts and proper memory management of variables in iOS, please use Instruments and do not check the Retain count facility as it is not very trustful.

To start using Instruments in Xcode 4, keep holding the Run button on the top left corner and then press on profile. This will make the Instruments come up.

Then there are two sections, which are Allocations and Leaks. Here, you can check the memory allocations and management of the variables.

Enjoy coding...! :)

0
AndersK On

because when you do

self.showDetail = detail;

you add one to the retain count if your property is declared with 'retain'

the setter handles the retain count for you, when you assign to the property the setter will increase the retain count for the object that you are assigning to the property. Similar when you assign nil to the property it will release it, i.e. decrement the retain count on the object.

0
Simon On

I'm guessing that your showDetail property has retain semantics. So when you do this:

self.showDetail = detail;

the synthesised property is calling retain. The other way to call your setter might make this clearer:

[self setShowDetail:detail]

So that's a retain count of 1. The second retain is being held by the array.