__bridge,__bridge_transfer and __bridge_retained in iOS

Source: Internet
Author: User


Reprinted from: Http://www.cocoachina.com/industry/20130411/5975.html Objective-c and Core Foundation objects convert memory management summary to each other

Posted: 2013-04-11 13:37 read: 4109

iOS allows easy conversion between OBJECTIVE-C and Core Foundation objects, with NSString and cfstringref for direct conversion without pressure: [CPP] View plaincopyprint? Cfstringref acfstring = (cfstringref) ansstring; NSString *anss.

"" Reading device

iOS allows easy conversion between OBJECTIVE-C and Core Foundation objects, with NSString and cfstringref for direct conversion without pressure:

[CPP] View plaincopyprint?
Cfstringref acfstring = (cfstringref) ansstring;
NSString *ansstring = (NSString *) acfstring;

For memory management issues, ARC can help manage Objective-c objects, but does not support the management of Core Foundation objects, so there is one problem to be aware of after the conversion: Who will release the object after use. This article focuses on the memory management after type conversion.

first, non-ARC memory management

If you do not use arc, manually manage the memory, the idea is clear, the use of finished, release converted objects can be.

[CPP] View plaincopyprint?
//nsstring Turn Cfstringref
Cfstringref acfstring = (cfstringref) [[NSString alloc] initwithformat:@ "%@", string];
03.//...
Cfrelease (acfstring);
05.
06.
.//cfstringref Turn NSString
Cfstringref acfstring = cfstringcreatewithcstring (Kcfallocatordefault,
Bytes,
nsutf8stringencoding);
NSString *ansstring = (NSString *) acfstring;
12.//...
[Ansstring release];

Second, the memory management under the arc

The birth of arc greatly simplifies our development efforts for memory management, but only supports the management of Objective-c objects and does not support Core Foundation objects. Core Foundation objects must use Cfretain and cfrelease for memory management. So when using Objective-c and Core Foundation objects to convert to each other, you must let the compiler know who is responsible for releasing the object and whether it is given to arc processing. Only correct handling can prevent a memory leak and double free cause the program to crash.

There are 3 different ways of conversion depending on the needs
__bridge (does not change object ownership)
__bridge_retained or Cfbridgingretain () (DE-ARC ownership)
__bridge_transfer or Cfbridgingrelease () (Give ARC ownership)


1. __bridge_retained or Cfbridgingretain ()
__bridge_retained or Cfbridgingretain () converts the Objective-c object into a core foundation object, bridging the object ownership to the core Foundation object, while depriving the arc of administrative power, The subsequent need for developers to use Cfrelease or related methods to manually release objects.

Example:

[CPP] View plaincopyprint?
Viewdidload-(void)
02. {
[Super Viewdidload];
04.
NSString *ansstring = [[NSString alloc]initwithformat:@ "test"];
Cfstringref acfstring = (__bridge_retained cfstringref) ansstring;
07.
(void) acfstring;
09.
10.//Correct practice should be performed cfrelease
//cfrelease (acfstring);
12.}

The program did not execute cfrelease, causing a memory leak:

Cfbridgingretain () is the __bridge_retained macro method, and the following two lines of code are equivalent:

[CPP] View plaincopyprint?
Cfstringref acfstring = (__bridge_retained cfstringref) ansstring;
Cfstringref acfstring = (cfstringref) cfbridgingretain (ansstring);


2. __bridge_transfer or Cfbridgingrelease ()


__bridge_transfer or cfbridgingrelease () converts non-objective-c objects into objective-c objects, while handing over the management of the object to arc, without the developer having to manually manage the memory.

Then the above example of the memory leak, and then into the OC object to the arc to manage the memory, no manual management, no memory leaks:

[CPP] View plaincopyprint?
Viewdidload-(void)
02. {
[Super Viewdidload];
04.
NSString *ansstring = [[NSString alloc]initwithformat:@ "test"];
Cfstringref acfstring = (__bridge_retained cfstringref) ansstring;
ansstring = (__bridge_transfer NSString *) acfstring;
08.}

Cfbridgingrelease () is the __bridge_transfer macro method, and the following two lines of code are equivalent:

[CPP] View plaincopyprint?
ansstring = (__bridge_transfer NSString *) acfstring;
ansstring = (NSString *) cfbridgingrelease (acfstring);

3. __bridge

__bridge is the most common type of conversion, and does not change the ownership of the object.

Managing memory from OC to CF,ARC:

[CPP] View plaincopyprint?
Viewdidload-(void)
02. {
[Super Viewdidload];
04.
NSString *ansstring = [[NSString alloc]initwithformat:@ "test"];
Cfstringref acfstring = (__bridge cfstringref) ansstring;
07.
(void) acfstring;
09.}

From CF to OC, you need to manually release the developer and not return to the arc tube:

[CPP] View plaincopyprint?
Viewdidload-(void)
02.
[Super Viewdidload];
04.
Cfstringref acfstring = cfstringcreatewithcstring (NULL, "test", kcfstringencodingascii);
NSString *ansstring = (__bridge NSString *) acfstring;
07.
(void) ansstring;
09.
Cfrelease (acfstring);
11.}

__bridge,__bridge_transfer and __bridge_retained in iOS

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.