在MacOS上使用AVFoudation从摄像机捕获视频时内存泄漏

时间:2019-01-09 08:30:07

标签: macos memory-leaks camera avfoundation

使用AVFoudation从摄像机捕获视频时,我发现内存泄漏。

我重复如下调用接口:alloc-init,startCapture,stopCapture和dealloc。 每个周期30k内存泄漏。 使用ARC可获得相同的结果。 仪器泄漏表明[AVCaptureSession startRunning]导致泄漏。 有什么问题吗?

#import "camera_capture.h"
@implementation CameraCapture
{
    AVCaptureSession*           _session;
    AVCaptureVideoDataOutput* _output;
    AVCaptureInput* _input;
    dispatch_queue_t _dataQueue;
}

-(id)init
{
    @autoreleasepool
    {
        self = [super init];
        [self initPrivate];
        return self;
    }
}

-(void)dealloc
{
    [_session beginConfiguration];
    [_session removeOutput:_output];
    [_session removeInput:_input];
    [_session commitConfiguration];

    dispatch_release(_dataQueue);
    _dataQueue = 0;

    [_output release];
    _output = nil;

    [_session release];
    _session = nil;
    NSLog(@"%s",__FUNCTION__);
    [super dealloc];
}

-(void)initPrivate
{
    _session = [AVCaptureSession new];
    NSArray* cameraDevices = [[NSArray alloc] initWithArray:[AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]];
    if(cameraDevices == nil || cameraDevices.count == 0)
    {
        return;
    }
    NSError* tmp_error;
        _input = [AVCaptureDeviceInput deviceInputWithDevice:(AVCaptureDevice*)[cameraDevices objectAtIndex:0] error:&tmp_error];
    if (_input == nil)
    {
        return;
    }
    [cameraDevices dealloc];
    cameraDevices = nil;
    _dataQueue = dispatch_queue_create("MyVideoDataQueue", DISPATCH_QUEUE_SERIAL);
    _output = [AVCaptureVideoDataOutput new];
    [_output setSampleBufferDelegate:self queue:_dataQueue];

    [_session beginConfiguration];
    [_session addInput:_input];
    [_session addOutput:_output];
    [_session commitConfiguration];
    return;
}

-(void)startCapture
{
    [_session startRunning];
}

-(void)stopCapture
{
   [_session stopRunning];
}


- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
   fromConnection:(AVCaptureConnection *)connection
{}

@end

0 个答案:

没有答案