I'm trying to pass an object of type id
to a NSInvocation
object. The compiler suggests I do a bridged cast like so:
[invocation setArgument:(__bridge void *)(argument) atIndex:idx];
Is that OK, and should I do anything else to prevent memory leaks or other problems?
Diagnose the Memory Leak Expand “Open Developer Tool,” and select “Instruments” Now choose “Leaks,” and make sure you have chosen your target app and device at the top (“Choose a profiling template for…”):
A memory leak occurs when the system is unable to determine if allocated space in memory is in use or not. Both Swift and Objective-C use Automatic Reference Counting (ARC) to manage space in memory. ARC is a memory-management system that keeps track of how many references there are to a given object.
__bridge transfers a pointer between Objective-C and Core Foundation with no transfer of ownership. __bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.
__bridge
will allow you (in loose terms) to go back and forth between Objective-C and C without any impact on the memory management of the object.
__bridge_transfer
is for transferring an object from C to Objective-C with a change in the object's ownership. It decreases the CF retain count and hands memory management over to ARC, which should be smart enough to subsequently retain the object in the manner necessary.
__bridge_retained
is essentially the inverse of __bridge_transfer
, it will pass from Objective-C to C and increment the object's retain count in the process. ARC will cease to be interested in the object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With