OS X: How to correctly bridge release pointer with __bridge_transfer

696 views Asked by At

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?

1

There are 1 answers

0
Darren On BEST ANSWER

When you use __bridge_retained or CFBridgingRetain(), 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 or CFBridgingRelease()), or you can simply call CFRelease().

In your case, just use CFRelease():

void ptr_release(const void *ptr) {
    CFRelease((CFTypeRef)ptr);
}