Thursday 15 March 2012

objective c - why lowercaseString of NSString is not released after the original NSString object is released? -



objective c - why lowercaseString of NSString is not released after the original NSString object is released? -

please check next code:

cfuuidref uuid=cfuuidcreate(kcfallocatordefault); nsstring *struuid=(nsstring *)cfuuidcreatestring(kcfallocatordefault,uuid); nsstring *loweruuid=[struuid lowercasestring]; nslog(@"struuid retaincount:%tu",[struuid retaincount]); cfrelease(struuid); cfrelease(uuid); nslog(@"loweruuid retaincount:%tu",[loweruuid retaincount]); nslog(@"loweruuid:%@",loweruuid);

run code in non-arc project, output be:

struuid retaincount:1 loweruuid retaincount:1 loweruuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx

lowercasestring property of nsstring, definition is:

@property (readonly, copy) nsstring *lowercasestring;

so it's reasonable infer if original nsstring object released, lowercasestring property released in nsstring's dealloc method. why in code above, loweruuid still there after original struuid released?

first of all, standard disclaimer: don't -retaincount. has many caveats utilize , can't trusted useful.

that said: inference not describe what's happening. when inquire struuid -lowercasestring, you're not getting sort of "child" object. resulting lowercased string independent object, own internal retain count. (it's possible original string retain own reference lowercased string, that's not concern.)

in code snippet, under standard memory management conventions, happening in general terms statement [struuid lowercasestring] returns "autoreleased" object. if original string released, string returned remains valid either: (a) at least until autorelease pool cleaned or (b) after lastly actual access, depending on arc status on project.

objective-c nsstring

No comments:

Post a Comment