views:

973

answers:

4

Do I need to release a Core Foundation objects to clear up memory? And if so, how?

For example, in the code:

ABAddressBookRef addressBook = ABAddressBookCreate();
CFArrayRef peopleArray = ABAddressBookCopyArrayOfAllPeople(addressBook);

do I need to release peopleArray? What about addressBook?

+6  A: 

Yes, in CoreFoundation you have to release anything with Create or Copy in the name. You do this with CFRelease(). In your case, you should be releasing both the array and the address book references.

Jason Coco
Thank you. I struggled to find the answer to this on Google. Probably because I was googling for "Core Framework" instead of "Core Foundation". Doh! :)
rein
A: 

I would suggest reading Apple's guide on Core Foundation memory management for an in-depth discussion of this. They have a similar guide for general Cocoa memory management as well.

To release a CF object, you would call the CFRelease function.

Marc W
+6  A: 

The rules for memory management in Core Foundation are similar to those in Cocoa: if the method that returns a reference contains the words "create" or "copy", you own the reference and must call CFRelease() on that reference to relinquish ownership. Otherwise, you do not own the reference and must call CFRetain to take ownership (necessarily requiring a subsequent CFRelease to relinquish that new ownership). These rules, as taken from the Memory Management Programming Guide for Core Foundation are:

  • If you create an object (either directly or by making a copy of another object—see “The Create Rule”), you own it.
  • If you get an object from somewhere else, you do not own it. If you want to prevent it being disposed of, you must add yourself as an owner (using CFRetain).
  • If you are an owner of an object, you must relinquish ownership when you have finished using it (using CFRelease).

In your example, both the addressBook and the peopleArray must be released. Since there is no autorelease equivalent in Core Foundation, if you are returning the a reference from a method, return the array without releasing it. You should (unless you're being evil) then include "create" in the method name to indicate to the caller that they now own a reference to the returned object. In this case, CFArray is toll-free bridged to NSCFArray, an Objective-C object that inherits from NSObject. You can thus cast peopleArray to an NSArray* and autorelease that, if you need to return it from a function/method:

return [(NSArray*)peopleArray autorelease];

Note that this only works for toll-free bridged classes. My understanding is that it's rather difficult to make your own toll-free bridged classes and only the primitive (string, array, etc.) CF classes are toll-free bridged, so this approach won't work always. Finally, if you can avoid using autorelease (i.e. you can make your memory management more explicit), that's probably always a good thing.

Barry Wark
Thanks Barry - I am well versed in the memory rules of Cocoa - I just wasn't sure if the CF rules were the same. Glad to know that they are.
rein
The same, except for the lack of autorelease... If you're comfortable with the Cocoa rules and avoid autorelease if possible, you're completely at home.
Barry Wark
Objects that are toll-free bridged may be autoreleased by casting to id: [(id)cfObject autorelease];
rpetrich
Right, so in this instance, the CFArrayRef could be cast to an NSArray* and autoreleased as in [(NSArray*)peopleArray autorelease]. I'll add that to the answer text.
Barry Wark
+3  A: 

Another small point that no-one has mentioned yet, some CF classes have a "toll-free bridge" with their NS counterpart. CFString and NSString, CFArray and NSArray are both examples. This is relevant as you can just use release with these classes.

See this other StackOverflow question for more information.

Stephen Darlington