first, the use of __bridge_retained
__bridge_retained conversion allows the variable to be converted to hold the assigned value of the object
void *p=0;
{
ID Obj=[[nsobject alloc] init];
p= (__bridge_retained void*) obj;
}
NSLog (@ "class=%@", [(__bridge ID) p class]);
Output Result:
Class=nsobject
Analysis:
After the variable scope ends, although obj fails, the __bridge_retained transformation causes the variable p to appear to hold the object's state, so the object is not freed
Second, __bridge_transfer
The __bridge_transfer keyword is used when you want to have a variable that has ownership of the object, after the type has been converted, to release the original ownership.
As in non-ARC environments:
ID obj = (id) p;
[obj retain];
[(ID) p release];
In the ARC environment, use the __bridge_transfer as follows:
ID obj = (__bridge_transfer id) p;
__bridge_retained is the compiler for us to do the retain operation, __bridge_transfer is the compiler for us to do the release
Third, the use of the Core Foundation framework
Corefoundation object is a C language implementation of the Corefoundation framework of the object, but also the concept of reference counting, the use of the keyword is cgretain/cfrelease,
Because it is the same as the foundation structure, the C language type can be converted under non-arc, as follows:
NSString *[email protected] "Richardyang";
Cfstringref strref= (cfstringref) str;
In the ARC environment, because the compiler manages the memory of the foundation object, but the Corefoundation object is not processed, the keyword __bridge/__bridge_retained is used for processing.
1. Using __bridge_retained
@autoreleasepool {
Cfmutablearrayref Cfobject=nil;
{
ID Obj=[[nsmutablearray alloc] init];
cfobject= (__bridge_retained cfmutablearrayref) obj;
Cfshow (Cfobject);
printf ("The Retain Count =%ld\n", Cfgetretaincount (Cfobject));
}
printf ("The Retain count is%ld\n", Cfgetretaincount (Cfobject));
Cfrelease (cfobject);//memory leaks if cfrelease is not performed
}
The output is:
The Retain Count =2
The Retain count is 1
2. Using __bridge
Do only type conversions, but do not modify object (memory) management rights;
@autoreleasepool {
Cfmutablearrayref Cfobject=nil;
{
ID Obj=[[nsmutablearray alloc] init];
cfobject= (__bridge cfmutablearrayref) obj;
Cfshow (Cfobject);
printf ("The Retain Count =%ld\n", Cfgetretaincount (Cfobject));
}
printf ("The Retain count is%ld\n", Cfgetretaincount (Cfobject));
Cfrelease (Cfobject);
}
Cfrelease (Cfobject); Throws an exception, __bridge implements the conversion, and does not hold the object.
3. Using __bridge_transfer
@autoreleasepool {
Cfmutablearrayref cfobject=cfarraycreatemutable (kcfallocatordefault, 0, NULL);
printf ("The Retaincount is%ld\n", Cfgetretaincount (Cfobject));
/*
* __bridge_transfer, the release operation is performed on Cfobject, and the object is assigned to obj, but Cfobject still points to the existing object, which can be used normally
*/
ID obj= (__bridge_transfer id) cfobject;
printf ("After __bridge_transfer Retaincount is%ld\n", Cfgetretaincount (Cfobject));
NSLog (@ "class=%@", obj);
}
/*
*obj out of scope, strong references fail, free objects, no memory leaks
*/
return 0;
__bridge_retained/__bridge_transfer/__bridge use of the detailed