开发者

Should I release this? Memory Management in Objective-C

Should I release strPhone? What about the coreFoundation object being cast to an NSString? What happens to it?

strPhone = [[NSString alloc] initWithUTF8String: [[(NSArra开发者_StackOverflow中文版y *)ABMultiValueCopyArrayOfAllValues(theProperty) objectAtIndex:identifier] UTF8String]];

Thanks for helping me understand.


You should release or autorelease both. For the NSString, any time you use alloc + init to create an object you are settings its reference count to 1. You are responsible for releasing it when done or autoreleasing it now to allow it to be released at the end of the run loop.

For the CFObject, ABMultiValueCopyArrayOfAllValues returns a CFArray which is “toll-free bridged” to NSArray (meaning it can be used interchangeably with NSArray). Anytime a copy is done - as is implied by the method's name, you are responsible for releasing the returned object. Again, you can release it immediately after you are done with it or autorelease it now to have it be released when the run loop completes.


Yes, both. See Apple's memory management guide for a complete but still pretty brief rundown of memory management in Cocoa.


Remember to NARC on your memory management.

New, Allocate, Retain, Copy. Those are the methods that create objects that YOU'RE responsible for releasing. Aside from those four methods, any new object you get is autoreleased and you don't have to explicitly handle its deallocation.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜