2013年12月4日星期三

[Android] Reference counting problem

NSString * string = @ "origion";
NSString * stringCopy = [string copy];
NSLog (@ "stringCopy RC =% lu", [stringCopy retainCount])

print results : stringCopy RC = 18446744073709551615
The above code is coming from the Internet copy , online analysis that RC = 2;
Who can you explain why not two ? In addition , the output string with NSLog reference count and stringCopy same.
I use Xcode 4.6 .
------ Solution ---------------------------------------- ----
because the string pointed to a constant @ "origion" strings, so the reference count is random
------ Solution ------------ --------------------------------
usually we do not need to specifically check an object's retain count is the number . Results of the query are often unexpected. Other objects you do not know the inside of the framework of the object you're interested in how many retain operation. In the debug memory management problems , you ensure that your code as long as concerns compliance with owner rules.
string constants is usually constant pool , not in the heap memory. So long as alloc, new, copy the corresponding release like. Do not need to know his retaincount.
------ eference --------------------------------------- < br> you put this NSLog (@ "stringCopy RC =% lu", [stringCopy retainCount]) into NSLog (@ "stringCopy RC =% i", [stringCopy retainCount]) to see look
------ eference ---------------------------------------

+1
------ eference ------------------------------- --------
Thank you for the answer .

没有评论:

发表评论