3

I have worked with manual ref counting, ARC and CF objects for a while, but while working with the AddressBook API today I realized I don't understand one thing (and I couldn't find an explanation in the documentation either).

Here is my usecase: I'm copying a ABMultiValueRef property from an ABRecordRef (to get all of the elements).

ABMultiValueRef multiString = ABRecordCopyValue(personRef, propertyID);
NSArray* multiValues = (__bridge_transfer NSArray *)ABMultiValueCopyArrayOfAllValues(multiString);
CFRelease(multiString);

//do something with the multiValues NSArray

So I got a copy of a certain property of type kABMultiStringPropertyType which presents itself as a CFArrayRef of CFStringRefs. If I understand correctly, I am responsible for releasing both the CFStringRefs in the array and the CFArrayRef itself. (Maybe not, so please let me know if I'm wrong here).

After bridging the CFArrayRef from the CF world and transferring ownership to ARC, I know the NSArray* will be taken care of by ARC. But what about the CFStringRefs inside that array? They are bridged to NSString* (I know that, because I can iterate the elements with for..in), but I'm not sure which type of __bridge* was applied to it.

From experience and me not encountering this earlier, I would guess that whatever __bridge* is used (__bridge or __bridge_transfer) on the array, the same one is used on the elements in that array.

Is that correct? Again, I couldn't find a confirmation anywhere, so if you know a place in the documentation which specifies it, I would greatly appreciate any info about it. Thanks.

//Edit after getting an answer

I did not realise both CFArray and NSArray retain their elements - and that's the case both in the CF and object worlds, so nothing has to change during the __bridge*. After transfered, the NSArray is taken care of by ARC and its elements are taken care of by the array itself. So when ARC releases the array, the array releases its elements (so ARC does not need to know anything about the elements).

1
  • The bridge directives simply supply ARC with information on what it should do relative to retain and release. In both CFArray and NSArray, objects are retained when added and released when removed, so ARC does nothing with them. I cannot think of any case where bridge affects anything other than the two objects.
    – David H
    Dec 12, 2013 at 12:16

1 Answer 1

2

__bridge_transfer or __bridge is "only" a type cast plus an optional transfer of ownership. The array itself it not modified by the brigde, and the array elements do not change at all.

So you are not responsible for releasing the array elements. The array retains its elements (ARC speak: keeps strong references), and releases them (removes the strong reference) when the elements are removed from the array.

Only if you explicitly transfer the ownership of an array element you are responsible for releasing it:

CFStringRef s = (__bridge_retain CFStringRef) multiValues[0];
// …
CFRelease(s);
1
  • Of course. For some reason I did not realise the array needs to retain its elements, meaning when ARC releases the array, the array "manually" releases its elements. This explains my question, thanks.
    – czechboy
    Dec 12, 2013 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.