CVMetalTextureGetTexture ownerhsip?

Jan*_*egg 5 objective-c core-foundation core-video automatic-ref-counting swift

我试图弄清楚所有权如何与功能一起工作CVMetalTextureGetTexture

CVMetalTextureRef textureRef;
// ... textureRef is created
id<MTLTexture> texture = CVMetalTextureGetTexture(_textureRef);
CVBufferRelease(textureRef); // Releasing the existing texture
// Is texture still valid here?
Run Code Online (Sandbox Code Playgroud)

texture仍然释放后有效textureRef?如果不是,我可以以某种方式将所有权从转让textureReftexture(ARC),这样我就不必CVBufferReleasetexture释放时再打电话了吗?

迅速回答同样的问题:

var texture: MTLTexture
do {
  var textureRef: CVMetalTexture
  // ... textureRef is created
  texture = CVMetalTextureGetTexture(textureRef)!
  // end of scope, textureRef is released
}
// Is texture still valid here?
Run Code Online (Sandbox Code Playgroud)

Bar*_*esh 1

这是一个很好的问题,因为这种类型破坏了Create Rule,但看起来这个方法确实保留了底层对象。我想这条规则不适用于 Core Foundation -> ARC 方法......

您可以在这个 Apple 演示中看到,他们在将引用转换为id<MTLTexture>. 他们在更隐式的 Swifty 版本中执行此操作,因此很容易被错过。