automatic-ref-countingobjective-c++toll-free-bridging

Does CFBridgingRelease restore ownership to preexisting references without direct assignment?


If I have the following code:

// objective C++ code .mm
id<MTLTexture> texture = ...;
void* ptr = (void*)CFBridgingRetain(texture);
share_ptr_with_native_code(ptr);
[texture do_stuff]; // is this valid?

// native code .cpp
void share_ptr_with_native(void* ptr)
{
  ptr->do_stuff();
  CFBridgingRelease(ptr);
}

Will texture be valid and retained by ARC again after the call to share_ptr_with_native()?


Solution

  • Other than various errors in your code snippet, yes, the line in question is valid. ARC continues to maintain its own strong reference to object while it's still in use in the top code, in addition to the one that you become responsible for. CFBridgingRetain() has a +1 effect on the retain count of the object, hence "retain" in its name.