views:

56

answers:

3

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

strPhone = [[NSString alloc] initWithUTF8String: [[(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty) objectAtIndex:identifier] UTF8String]];

Thanks for helping me understand.

+2  A: 

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

Chuck
+3  A: 

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.

Peter
Don't worry about reference counts. If you create an object by allocating it explicitly (`alloc`/`Create`) or by copying another object, you own it. Whatever you own, you must release. The Memory Management Programming Guide was recently rewritten along these lines: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/MemoryMgmt In this case, the CF version is also relevant: http://developer.apple.com/mac/library/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/
Peter Hosey
+1  A: 

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.

Dan Ray