开发者

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

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....


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.


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...! :)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜