Jesse Bunch
Jesse Bunch

Reputation: 6679

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: [[(NSArray *)ABMultiValueCopyArrayOfAllValues(theProperty) objectAtIndex:identifier] UTF8String]];

Thanks for helping me understand.

Upvotes: 0

Views: 274

Answers (3)

Dan Ray
Dan Ray

Reputation: 21893

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.

Upvotes: 1

Peter
Peter

Reputation: 1031

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.

Upvotes: 3

Chuck
Chuck

Reputation: 237060

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

Upvotes: 2

Related Questions