How to use "CFRetain(sampleBuffer)" and "CFRelease(sampleBuffer)" in Swift?
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 the CMSampleBuffer object outside of the scope of this method, you must CFRetain it and then CFRelease it when you are finished with it. (Apple Document)
According to Apple Doc
Memory Managed Objects
Core Foundation objects returned from annotated APIs are automatically memory managed in Swift—you do not need to invoke the CFRetain, CFRelease, or CFAutorelease functions yourself.
If you return Core Foundation objects from your own C functions and Objective-C methods, 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 enclose C function declarations that follow Core Foundation ownership policy naming policy in order to infer memory management from naming.
If you use only annotated APIs that do not indirectly return Core Foundation objects, you can skip the rest of this section. Otherwise, continue on to learn about working with unmanaged Core Foundation objects.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With