0

如果我有以下代码:

// 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);
}

在调用?后将texture再次有效并由 ARC 保留share_ptr_with_native()

4

2 回答 2

2

即使说的都是对的,如果你改变你的

CFBridgingRelease(ptr);

CFRelease(ptr).

__bridge_retained 或 CFBridgingRetain 将 Objective-C 指针转换为 Core Foundation 指针,并将所有权转移给您。您负责调用 CFRelease 或相关函数来放弃对象的所有权。

取自https://developer.apple.com/library/content/documentation/CoreFoundation/Conceptual/CFDesignConcepts/Articles/tollFreeBridgedTypes.html

于 2018-03-03T11:39:34.170 回答
2

除了代码片段中的各种错误之外,是的,有问题的行是有效的。ARC 继续保持其自己的强引用,object而它仍在顶级代码中使用,除了您负责的那个。CFBridgingRetain()对对象的保留计数有 +1 影响,因此其名称中包含“保留”。

于 2018-03-02T04:23:55.983 回答