Memory Management Summary of the conversion between ZZZ objective-C and core Foundation objects

Source: Internet
Author: User

Objective-C Memory Management Summary of mutual conversion between core Foundation objects

 

 

IOS allows easy conversion between objective-C and core Foundation objects. For nsstring and cfstringref, direct conversion is free of pressure:

 

  1. Cfstringref acfstring = (cfstringref) ansstring;
  2. Nsstring * ansstring = (nsstring *) acfstring;



 

For memory management problems, arc can help manage objective-C objects, but it does not support the management of core Foundation objects. Therefore, you must pay attention to the following question after conversion: Who will release the used objects. This article focuses on Memory Management after type conversion.

 

 

1. Non-arc Memory Management


If you do not use arc and manually manage the memory, the idea is clear. After using it, you can use the release converted object.

 

  1. // Nsstring to cfstringref
  2. Cfstringref acfstring = (cfstringref) [[nsstring alloc] initwithformat: @ "% @", string];
  3. //...
  4. Cfrelease (acfstring );
  5. // Convert cfstringref to nsstring
  6. Cfstringref acfstring = cfstringcreatewithcstring (kcfallocatordefault,
  7. Bytes,
  8. Nsutf8stringencoding );
  9. Nsstring * ansstring = (nsstring *) acfstring;
  10. //...
  11. [Ansstring release];



 

 

II. The birth of the memory management Arc Under the arc greatly simplifies our development work on 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. When objective-C and core Foundation objects are used to convert each other, the compiler must know who is responsible for releasing the object and whether the object is handed over to arc for processing. Only correct processing can avoid Memory leakage and Double Free causing program crash. There are three conversion methods based on different requirements.
  • _ Bridge (object ownership is not changed)
  • _ Bridge_retained or cfbridgingretain () (remove arc ownership)

  • _ Bridge_transfer or cfbridgingrelease ()(

    Give arc ownership)
1. _ bridge_retained or cfbridgingretain () _ bridge_retained or cfbridgingretain, in the future, developers need to manually release objects using cfrelease or related methods. Let's look at an example:
  1. -(Void) viewdidload
  2. {
  3. [Super viewdidload];
  4. Nsstring * ansstring = [[nsstring alloc] initwithformat: @ "test"];
  5. Cfstringref acfstring = (_ bridge_retained cfstringref) ansstring;
  6. (Void) acfstring;
  7. // The correct method should be cfrelease.
  8. // Cfrelease (acfstring );
  9. }
The program does not execute cfrelease, causing memory leakage: cfbridgingretain () is the macro method of _ bridge_retained. The following two lines of code are equivalent:
  1. Cfstringref acfstring = (_ bridge_retained cfstringref) ansstring;
  2. Cfstringref acfstring = (cfstringref) cfbridgingretain (ansstring );


2. _ bridge_transfer, cfbridgingrelease () _ bridge_transfer, or cfbridgingrelease () converts non-objective-C objects to objective-C objects, and grants the Object Management Right to arc, developers do not need to manually manage the memory. Then, the above example of Memory leakage is converted into an OC object and handed over to the Arc for memory management, without manual management or memory leakage:
  1. -(Void) viewdidload
  2. {
  3. [Super viewdidload];
  4. Nsstring * ansstring = [[nsstring alloc] initwithformat: @ "test"];
  5. Cfstringref acfstring = (_ bridge_retained cfstringref) ansstring;
  6. Ansstring = (_ bridge_transfer nsstring *) acfstring;
  7. }

Cfbridgingrelease () is the macro method of _ bridge_transfer. The following two lines of code are equivalent:
  1. Ansstring = (_ bridge_transfer nsstring *) acfstring;
  2. Ansstring = (nsstring *) cfbridgingrelease (acfstring );
3. _ bridge _ Bridge only performs type conversion without changing the object ownership. It is our most common conversion character. From oC to CF, arc memory management:
  1. -(Void) viewdidload
  2. {
  3. [Super viewdidload];
  4. Nsstring * ansstring = [[nsstring alloc] initwithformat: @ "test"];
  5. Cfstringref acfstring = (_ bridge cfstringref) ansstring;
  6. (Void) acfstring;
  7. }


To switch from CF to OC, developers need to manually release it, not under the control of arc:
  1. -(Void) viewdidload
  2. {
  3. [Super viewdidload];
  4. Cfstringref acfstring = cfstringcreatewithcstring (null, "test", kcfstringencodingascii );
  5. Nsstring * ansstring = (_ bridge nsstring *) acfstring;
  6. (Void) ansstring;
  7. Cfrelease (acfstring );
  8. }

Memory Management Summary of the conversion between ZZZ objective-C and core Foundation objects

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.