Objective-C and Core Foundation objects convert each other
IOS allows easy conversion between Objective-C and Core Foundation objects:
- CFStringRef aCFString = (CFStringRef) aNSString;
- 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.
- // NSString to CFStringRef
- CFStringRef aCFString = (CFStringRef) [[NSString alloc] initWithFormat: @ "% @", string];
- //...
- CFRelease (aCFString );
-
-
- // Convert CFStringRef to NSString
- CFStringRef aCFString = CFStringCreateWithCString (kCFAllocatorDefault,
- Bytes,
- NSUTF8StringEncoding );
- NSString * aNSString = (NSString *) aCFString;
- //...
- [ANSString release];
2. Memory Management under ARC
The birth of ARC greatly simplifies our development for memory management, but only supports the management of Objective-C objects, 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.
Based on different requirements, there are three conversion methods: _ 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:
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
-
- NSString * aNSString = [[NSString alloc] initWithFormat: @ "test"];
- CFStringRef aCFString = (_ bridge_retained CFStringRef) aNSString;
-
-
-
- // The correct method should be CFRelease.
- // CFRelease (aCFString );
- }
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:
- 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 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:
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
-
- NSString * aNSString = [[NSString alloc] initWithFormat: @ "test"];
- CFStringRef aCFString = (_ bridge_retained CFStringRef) aNSString;
- ANSString = (_ bridge_transfer NSString *) aCFString;
- }
CFBridgingRelease () is the macro method of _ bridge_transfer. The following two lines of code are equivalent:
- ANSString = (_ bridge_transfer NSString *) aCFString;
- ANSString = (NSString *) CFBridgingRelease (aCFString );
3. _ bridge
_ Bridge only performs type conversion without changing the ownership of objects. It is our most common conversion character.
From OC to CF, ARC memory management:
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
-
- NSString * aNSString = [[NSString alloc] initWithFormat: @ "test"];
- CFStringRef aCFString = (_ bridge CFStringRef) aNSString;
-
-
- } To switch from CF to OC, developers need to manually release it, not under the control of ARC:
- -(Void) viewDidLoad
- {
- [Super viewDidLoad];
-
- CFStringRef aCFString = CFStringCreateWithCString (NULL, "test", kCFStringEncodingASCII );
- NSString * aNSString = (_ bridge NSString *) aCFString;
-
-
-
- CFRelease (aCFString );
- }