objective-cmemory-managementcore-foundation

Proper way to CFRelease CFMutableArrayRef contents


I am using CFMutableArrayRef in my code. There I am adding several CFTypeRef elements.

When I am done using the array, do I need to call CFArrayRemoveAllValues(myArray); before actually releasing the array or simply calling to CFRelease(myArray); will deallocate array contents as well?

Thanks in advance


Solution

  • Calling CFRelease is all you need to do, assuming the objects contained in the array are managed appropriately.

    CFArray is open-source and you can see the implementation yourself at https://opensource.apple.com/source/CF/CF-1153.18/CFArray.c.auto.html (look for __CFArrayDeallocate and __CFArrayReleaseValues).

    CFArray is toll-free bridged with NSArray so in Objective-C it's rarely necessary to use CFArray directly.