I have a block object that is converted to a void *
pointer to pass to a C library. I'm deploying to OS X 10.6 so CFBridgingRetain
and CFBridgingRelease
are not available.
Instead I'm doing:
void ptr_retain(void (^block)()) {
void *ptr = (__bridge_retained void *)(block);
...
// pass ptr to C lib
}
void ptr_release(const void *ptr) {
// C lib calls this function
void (^block)() = (__bridge_transfer void(^)())ptr;
}
Question:
In ptr_release I get an "unused variable" warning. I'm concerned the compiler may optimize out the __bridge_transfer
line and the block will never be released. Could this happen? Even if it doesn't, is this the correct way to let ARC know to release the block at the end of ptr_release
?
When you use
__bridge_retained
orCFBridgingRetain()
, you get a pointer with a +1 retain count. You are responsible for releasing this reference.You can release the reference by transferring ownership back to ARC (using
__bridge_transfer
orCFBridgingRelease()
), or you can simply callCFRelease()
.In your case, just use
CFRelease()
: