Can ARC manage Core Foundation objects without connection fees?

If I have a non- Core Foundation bridge object , can I safely transfer ownership of ARC, or is this privilege reserved for free types?

For example:

- (id)myBundle {
    CFBundleRef b = CFBundleCreate(NULL, self.bundleURL);
    return b == NULL ? nil : (__bridge_transfer id)b;
}

- (UInt32)myBundleVersionNumber {
    return CFBundleGetVersionNumber((__bridge CFBundleRef)self.myBundle);
}

      

+3


source to share


1 answer


Each CoreFoundation object is also an Objective-C object. In the old days, before it CFAutoRelease()

was introduced in 10.9, non-ARC code would auto-update CoreFoundation objects using idiom [(id)<CFTypeRef> autorelease]

; which was possible due to CoreFoundation objects being Objective-C objects.

The correspondence is that in ARC, the CoreFoundation object can be __bridge_transfer

'ed before ARC.

So yes, you can safely transfer ownership of any CoreFoundation object to ARC.



If you go through your assembly-level example code, you will find an ARC call _objc_release

, which in turn will call CFRelease

.

Note. Unaided bridging objects are those where there are exact equivalents in CoreFoundation and Objective-C, so they can be used interchangeably.

+5


source







All Articles