How to release the texture from CVMetalTextureCacheCreateTextureFromImage created?

Hi

I have create the texture using CVMetalTextureCacheCreateTextureFromImage, It's looks like return a UnsafeMutablePointer<CVMetalTexture?>, and I pass it to metal shader using CVMetalTextureGetTexture. use this way looks like have memory leak, how to release the texture is correct way? I tried call CVMetalTextureCacheFlush but it not working.

Thanks Chengliang

Part of code: var texture: CVMetalTexture? = nil let status = CVMetalTextureCacheCreateTextureFromImage(kCFAllocatorDefault, textureCache, pixelBuffer, nil, pixelFormat, width, height, planeIndex, &texture)

...

encoder.setTexture(CVMetalTextureGetTexture(self.texture), index: 0)

Add more info

here is the code of CVPixelBuffer

   let attributes : [NSObject:AnyObject] = [    kCVPixelBufferCGImageCompatibilityKey : true as AnyObject,    kCVPixelBufferCGBitmapContextCompatibilityKey : true as AnyObject,    kCVPixelBufferIOSurfacePropertiesKey: [     "IOSurfaceOpenGLESFBOCompatibility": true,     "IOSurfaceOpenGLESTextureCompatibility": true,     "IOSurfaceCoreAnimationCompatibility": true     ] as AnyObject   ]       CVPixelBufferCreate(kCFAllocatorDefault,             Int(width), Int(height),             kCVPixelFormatType_420YpCbCr8BiPlanarFullRange,             attributes as CFDictionary,             &pixelBuffer)    

Hi Chengliang,

CVMetalTextureGetTexture does not allocate memory. Rather, it provides you with a pointer to the internal Metal Texture object, so there is no risk of leaking memory by calling this function.

One way to verify the object does not allocate memory is by checking its name. In this case, because it does not contain the word "Create", it is safe to assume it does not allocate memory.

When in doubt, I recommend you use the Leaks tool in Instruments. When you launch your program through it, it will helpfully flag any leaked memory, and allow you to review the stack trace for its allocation.

How to release the texture from CVMetalTextureCacheCreateTextureFromImage created?
 
 
Q