Shallow solution to __bridge, __bridge_retained and __bridge_transfer in Arc

Source: Internet
Author: User

Article Source: http://www.outflush.com/2015/03/introduction-of-arc-bridge-type-transfer/

Before you explain bridge-related modifiers, you should first understand the following

    • The Core Foundation is a set of C-language interfaces that provide interfaces to the same functionality as the foundation, but only the Objective-c interface provided by the Foundation framework.
    • The object in the Core Foundation also has the concept of reference counting, similar to the retain/release of the Foundation, and its corresponding interface is Cfretain/cfrelease
    • The objects of these two frames can be converted to each other, and this conversion is called toll-free bridge
    • When using Arc, objects in the core Foundation are not managed by arc, so the objects in the core Foundation and foundation involve conversion of object ownership in the process of converting to each other, and the bridge modifier is used here.
Conversion between a normal object and a C language pointer
voidNULL;{id obj = [[NSObjectvoid *)obj;}NSLog(@"Hello");// 这里会出现错误NSLog(@"%@"id)p class]);

The obj in the above code is managed by arc, and P is a C language pointer that is not within the scope of ARC management. When the program executes beyond the scope of obj, Arc assigns obj to the release, when the p pointer becomes null. Therefore, when using __bridge , it is necessary to know the life cycle of the object or else a similar error will occur.

You should then use the __bridge_retain keyword to convert

void *)obj;// 上面这段代码在非ARC的环境下可以表示为p = obj;[(id)p retain];

So when obj is in Arc release, the p pointer still points to a valid object.

The __bridge_transfer is used to convert a C-language pointer from a __bridge_retain conversion to a normal object managed by the arc.

idid)p;// 用非ARC来表示就是id obj = p;[obj retain];[(id)p release];

As you can see,__bridge_transfer transfers ownership of the object that P points to to the arc-managed obj.

when declaring ID obj in an ARC environment, the default is the strong modifier, so arc automatically retain the obj, so that __bridge_transfer only does release processing.
Transformation between the Core Foundation and the general object of the foundation

It is known from above that objects in the core Foundation also have the concept of reference counting. In non-ARC environments, Core Foundation objects and foundation objects can be converted (toll-free bridge) through standard C language type conversions. When you introduce arc, you need bridge to convert, because you need to explicitly tell the compiler how to handle the ownership of the object.

For example:

NSString *str = [NSString stringWithFormat…];CFStringRef cfStr = (__bridge CFStringRef)str;...CFRelease(cfStr);

Here the Str object is managed by arc, and CFSTR is not in the management of the arc, because the __bridge is simply a type conversion, so when STR is in Arc release, CFSTR becomes null.

When the above code is converted using __bridge_retain , Cfstr has ownership of the Str object, if STR is still valid by Arc release,cfstr. However, because the object in the core Foundation also has a reference counting concept, you need to manually release the CFSTR using cfrelease () . The code is as follows:

NSString *str = [NSString stringWithFormat…];CFStringRef cfStr = (__bridge_retain CFStringRef)str;...CFRelease(cfStr);

As for __bridge_transfer, you can tell from the above that it is used to transfer ownership of the object, so the CF (Core Foundation) object is using __bridge_transfer is freed when converted to a foundation object.

CFStringRef cfStr = CFStringCreate…();NSStringNSString *)cfStr;// 在非ARC环境下,上面这句等同于NSString *str = cfStr;CFRelease(cfStr);

In fact, there are two functions within the core foundation that are used for the conversion of CF Objects and foundation objects

CFTypeRef CFBridgingRetain(id X) {return (__bridge_retained CFTypeRef)X;}id CFBridgingRelease(CFTypeRef X) {returnid)X;}

You can also use these two functions to convert a type between objects.

Summarize
    • Bridge is used for conversion between ARC-managed objects and objects not managed by arc
    • The __bridge is only responsible for simple type conversions and requires extra attention to the life cycle of the object.
    • __bridge_retain transforms an arc-managed object into an object that is not managed by ARC, retain the Arc-managed object, making it part of an object that is not managed by arc (description is inappropriate and self-guessing).
    • __bridge_transfer objects that are not managed by ARC are converted to ARC-managed objects and are not subject to the ARC-managed object release.

Shallow solution to __bridge, __bridge_retained and __bridge_transfer in Arc

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.