CVMetalTextureGetTexture ownerhsip?
Asked Answered
S

2

8

I'm trying to figure out how the ownership works with the function CVMetalTextureGetTexture:

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

Is texture still valid after releasing textureRef? If not, can I somehow transfer ownership from textureRef to texture (ARC), so I don't have to call CVBufferRelease later when texture is released?

The same question for 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?
Sluiter answered 6/11, 2017 at 16:3 Comment(2)
You might want to have a look at the file MetalCameraSession.swift of this GitHub Reporsitory.Diversification
@Diversification Thanks for the comment! I can't be sure, though, if the guy at this repository did everything correctly, or has memory issues with his code...Hatten
C
1

This is an excellent question since this kind of breaks the Create Rule, but it seems like this method indeed retains the underlying object. I guess this rule doesn't apply for Core Foundation -> ARC methods...

You can see in this Apple Demo that they do release the ref after converting it to an id<MTLTexture>. They do it in the more implicit Swifty version so it's easily missed.

Castanon answered 27/8, 2020 at 7:56 Comment(0)
B
1

We have also made another experiment before I see your question:

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

I found that I did not release the texturePtr, but still no memory leak occurs. What's more, texturePtr is valid in sometime and before we replace it with the new var.
So I think maybe this is linked to your question.
My reply is more like an argument other than an answer.

Bresnahan answered 13/11, 2018 at 3:21 Comment(0)
C
1

This is an excellent question since this kind of breaks the Create Rule, but it seems like this method indeed retains the underlying object. I guess this rule doesn't apply for Core Foundation -> ARC methods...

You can see in this Apple Demo that they do release the ref after converting it to an id<MTLTexture>. They do it in the more implicit Swifty version so it's easily missed.

Castanon answered 27/8, 2020 at 7:56 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.