Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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)

like image 932
Limon Avatar asked Jul 01 '16 07:07

Limon


1 Answers

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.

like image 177
Jack Avatar answered Sep 22 '22 18:09

Jack