How to use "CFRetain (sampleBuffer)" in Swift?

How to use "CFRetain (sampleBuffer)" and "CFRelease (sampleBuffer)" in Swift? enter image description here CFRetain is unavailable: Core Foundation objectes are automatically memory managed.

  - (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection { [self appendVideoSampleBuffer:sampleBuffer]; } - (void)appendVideoSampleBuffer:(CMSampleBufferRef)sampleBuffer { dispatch_async( _writingQueue, ^{ CFRetain(sampleBuffer); [_videoInput appendSampleBuffer:sampleBuffer]; CFRelease(sampleBuffer); }); } 

If you need to reference a CMSampleBuffer object outside the scope of this method, you must CFRetain it and then CFRelease when you are done with it. ( Apple document )

+6
source share
2 answers

According to Apple Doc

Managed Memory Objects

Core Foundation objects returned from annotated APIs are automatically managed in Swift β€” you don’t need to reference CFRetain, CFRelease or CFAutorelease function independently.

If you return Core Foundation objects from your own C and Objective-C functions, you can annotate them with either the CF_RETURNS_RETAINED or CF_RETURNS_NOT_RETAINED macro to automatically insert memory management calls. You can also use the CF_IMPLICIT_BRIDGING_ENABLED and CF_IMPLICIT_BRIDGING_DISABLED macros to include C function declarations that follow the Core Foundation ownership policy naming policy to derive memory management on behalf of.

If you use only annotated APIs that do not indirectly return Core Foundation Objects, you can skip the rest of this section. Otherwise, continue working with unmanaged Core Foundation objects.

+1
source

You just need sampleBuffer = nil manually release. But it is recommended that you copy the pointer before using it with CMSampleBufferCreateCopy(kCFAllocatorDefault, sampleBuffer, &newBuffer) . In this case, set newBuffer to nil before creating a copy of the new sampleBuffer .

0
source

All Articles