我试图弄清楚所有权如何与函数CVMetalTextureGetTexture一起使用:
CVMetalTextureRef textureRef;
// ... textureRef is created
id<MTLTexture> texture = CVMetalTextureGetTexture(_textureRef);
CVBufferRelease(textureRef); // Releasing the existing texture
// Is texture still valid here?
释放textureRef后纹理是否仍然有效?如果没有,我可以以某种方式将所有权从textureRef转移到纹理(ARC),所以我不必在以后释放纹理时调用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?
最佳答案 在我看到你的问题之前,我们还做了另一个实验:
在循环中,从CVPixelBuffer创建的CVMetalTextureRef来自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;
}
我发现我没有释放texturePtr,但仍然没有发生内存泄漏.更重要的是,texturePtr在某个时候和我们用新的var替换之前是有效的.
所以我想也许这与你的问题有关.
我的回答更像是一个不是答案的争论.