【问题标题】:CVMetalTextureGetTexture ownerhsip?MetalTextureGetTexture 所有权?
【发布时间】:2018-04-18 20:36:54
【问题描述】:

我正在尝试弄清楚所有权如何与函数 CVMetalTextureGetTexture 一起使用:

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

释放textureReftexture还有效吗?如果没有,我可以以某种方式将所有权从textureRef 转移到texture (ARC),这样我就不必在稍后释放texture 时调用CVBufferRelease

对于 swift 的同样问题:

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

【问题讨论】:

  • 你可能想看看这个GitHub Reporsitory的文件MetalCameraSession.swift
  • @mschmidt 感谢您的评论!不过,我不能确定这个存储库中的那个人是否正确地完成了所有操作,或者他的代码是否存在内存问题......

标签: objective-c swift automatic-ref-counting core-foundation core-video


【解决方案1】:

在看到你的问题之前,我们还做了另一个实验:

in a loop, CVMetalTextureRef created from CVPixelBuffer comes from Camera.

static void *texturePtr;  

/** AVCaptureVideoDataOutputSampleBufferDelegate, as a loop */
{
CVMetalTextureRef textureRef;
// ... textureRef is created
id<MTLTexture> texture = CVMetalTextureGetTexture(_textureRef);
CVBufferRelease(textureRef); // Releasing the existing texture

texturePtr= (__bridge_retained void*)texture;

// no releasing the texturePtr
// (__bridge_transfer id<MTLTexture>)texturePtr;
}

我发现我没有releasetexturePtr,但仍然出现no memory leak。更重要的是,texturePtrvalid 在我们用新的 var 替换它之前的某个时间。
所以我认为这可能与您的问题有关。
我的回复更像是一个论点而不是一个答案。

【讨论】:

    【解决方案2】:

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

    您可以在this Apple Demo 中看到他们确实在将 ref 转换为id&lt;MTLTexture&gt; 后释放了它。他们在更隐含的 Swifty 版本中执行此操作,因此很容易被忽略。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-03
      • 2012-07-07
      • 2015-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-20
      • 2017-06-03
      相关资源
      最近更新 更多