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
- __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.
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