Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must I use __bridge or __bridge_retained if I'm bridging an autoreleased object to Core Foundation?

The ARC Migration Tool is having trouble with this:

NSURL *fileURL = [NSURL fileURLWithPath:path];
AudioFileOpenURL((CFURLRef)fileURL, kAudioFileReadPermission, 0, &fileID);

In particular, it isn't sure about if it should do a __bridge or __bridge_retained. And I'm either.

-fileURLWithPath returns an autoreleased object, and in this place I'm not the owner of fileURL. But at the same time, the object has a retain count of at least +1.

I'd bet this has to be done with __bridge only.

like image 590
openfrog Avatar asked Jan 12 '12 21:01

openfrog


People also ask

When to use__ bridge?

__bridge is used to transfer/convert the pointer between Core Foundation and Foundation without ownership.

What does__ bridge do?

__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership. __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.


1 Answers

You want to use the regular __bridge cast only for this. You would use __bridge_retained only if you want to manage the lifecycle of a cast CF object. For example:

CFStringRef cf_string = (__bridge_retained CFStringRef)someNSString;
// some long time later, perhaps in another method etc
CFRelease(cf_string);

So the __bridge_retained is really telling the compiler that you had an ARC object and now you want to basically turn it into a CF object that you're going to manage directly.

like image 53
Jason Coco Avatar answered Oct 21 '22 03:10

Jason Coco