IOSurface objects aren't released in ScreenCaptureKit

I use ScreenCaptureKit, CoreVideo, CoreImage, CoreMedia frameworks to capture screenshots on macOS 14.0 and higher.

Example of creating CGImageRef:

     CVImageBufferRef cvImageBufferRef = ..;

      CIImage* temporaryImage = [CIImage imageWithCVPixelBuffer:cvImageBufferRef];
        
      CIContext* temporaryContext = [CIContext context];

      CGImageRef imageRef = [temporaryContext createCGImage:temporaryImage
                                                     fromRect:CGRectMake(0, 0,
                                                                       CVPixelBufferGetWidth(cvImageBufferRef),
                                                                      CVPixelBufferGetHeight(cvImageBufferRef))];

I have the next results of profiling with XCode Instruments Memory Leaks & Allocations: there is constantly increasing memory usage, but no memory leaks are detected, and there are many calls to create IOSurface objects, that have been never released. The most part of memory - All Anonymous VM - VM: IOSurface. The heaviest stack trace:

[RPIOSurfaceObject initWithCoder:]
[IOSurface initWithMachPort:]
IOSurfaceClientLookupFromMachPort

I don't have any of IOSurface objects created by myself. There are low-level calls to it. In Allocation List I can see many allocations of IOSurface objects, but there are no info about releasing it.

Due to this info, how can I release them to avoid permanent increasing memory consumption?

How are you getting the CVImageBufferRef? Are you releasing it after you are done? Try to run the clang static analyzer in Xcode.

Thank you for your question. Got CVImageBufferRef in this way:

CMSampleBufferRef sampleBuffer;
CVImageBufferRef cvImageBufferRef = CMSampleBufferGetImageBuffer(sampleBuffer);

Getting CVImageBufferRef in this way, I do not own the returned CVImageBufferRef. Therefore, I shouldn't release it. Also, I tried to release CMSampleBuffer in this way:

    {
        CFRelease(_sampleBuffer);
        _sampleBuffer = NULL;
    }

But I got crash when CFRelease is called.

IOSurface objects aren't released in ScreenCaptureKit
 
 
Q