CFBridgingRelease 是否在没有直接分配的情况下将所有权恢复到预先存在的引用?
Does CFBridgingRelease restore ownership to preexisting references without direct assignment?
如果我有以下代码:
// 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);
}
调用share_ptr_with_native()
后,texture
是否有效并被ARC再次保留?
除了您的代码片段中的各种错误,是的,有问题的行是有效的。 ARC 继续维护它自己对 object
的强引用,同时它仍在顶级代码中使用,除了您负责的代码。 CFBridgingRetain()
对对象的保留计数有 +1 影响,因此在其名称中有 "retain"。
说的都对,要是改一下就更好了
CFBridgingRelease(ptr);
到
CFRelease(ptr)
.
__bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you.
You are responsible for calling CFRelease or a related function to relinquish ownership of the object.
如果我有以下代码:
// 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);
}
调用share_ptr_with_native()
后,texture
是否有效并被ARC再次保留?
除了您的代码片段中的各种错误,是的,有问题的行是有效的。 ARC 继续维护它自己对 object
的强引用,同时它仍在顶级代码中使用,除了您负责的代码。 CFBridgingRetain()
对对象的保留计数有 +1 影响,因此在其名称中有 "retain"。
说的都对,要是改一下就更好了
CFBridgingRelease(ptr);
到
CFRelease(ptr)
.
__bridge_retained or CFBridgingRetain casts an Objective-C pointer to a Core Foundation pointer and also transfers ownership to you. You are responsible for calling CFRelease or a related function to relinquish ownership of the object.