Objective-c:objective-c and Core Foundation objects convert memory management to each other

Source: Internet
Author: User

objective-c and Core Foundation objects convert memory management to each other

iOS allows easy conversion between OBJECTIVE-C and Core Foundation objects, taking nsstring and Cfstringref as a direct conversion to no pressure:

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

//...    
Cfrelease (acfstring);   
  
  
//cfstringref to NSString    
Cfstringref acfstring = cfstringcreatewithcstring (kcfallocatordefault,  
                         ,         &NB Sp               BYTES, &NBSP
              &N Bsp                          ,         NSU tf8stringencoding);   
NSString *ansstring = (NSString *) acfstring;  
//...    
[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

    1. __bridge (does not change object ownership)
    2. __bridge_retained or Cfbridgingretain () (DE-ARC ownership)
    3. __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.

Let's look at an example:

-(void) viewdidload
{
[Super Viewdidload];

NSString *ansstring = [[NSString alloc]initwithformat:@ "test"];
Cfstringref acfstring = (__bridge_retained cfstringref) ansstring;

(void) acfstring;

The right approach should be performed cfrelease
Cfrelease (acfstring);
}

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:

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:

-(void) viewdidload
{
[Super Viewdidload];

NSString *ansstring = [[NSString alloc]initwithformat:@ "test"];
Cfstringref acfstring = (__bridge_retained cfstringref) ansstring;
ansstring = (__bridge_transfer NSString *) acfstring;
}

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

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:

-(void) viewdidload
{
[Super Viewdidload];

NSString *ansstring = [[NSString alloc]initwithformat:@ "test"];
Cfstringref acfstring = (__bridge cfstringref) ansstring;

(void) acfstring;
}

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

-(void) viewdidload
{
[Super Viewdidload];

Cfstringref acfstring = cfstringcreatewithcstring (NULL, "test", kcfstringencodingascii);
NSString *ansstring = (__bridge NSString *) acfstring;

(void) ansstring;

Cfrelease (acfstring);
}

Objective-c:objective-c and Core Foundation objects convert memory management to each other

Related Article

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.