Я обнаружил утечки памяти при использовании AVFoudation для захвата видео с камеры.
Я повторяю вызов интерфейса следующим образом: alloc-init, startCapture, stopCapture и dealloc.
Утечка памяти 30к каждый цикл.
Используя 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