AVCaptureSession get Memory warning and crash with no reason
Asked Answered
S

2

15

I am working on an app that manipulates HD photos. I am taking a photo with an AVCaptureSession, stopping it and then apply effects on that photo.

The thing that makes me CRAZY is that everything works fine, instruments tells me that I release all the memory I use properly and on time. It goes really high yes, sometimes to 100mb. But it goes down quickly.

Then I restart my Capture Session and I got a memory warning. There is absolutely no reason for that ;_; All the memory I used if freed... Then the next time I will restart the capture session the app crashes. No messages, no logs, nothing at all.

I don't know how to solve this, I don't know where to look for... If someone could help me a little bit I would be so glad!

Thanks in advance!

Superaltar answered 10/3, 2012 at 8:1 Comment(0)
C
23

I've had the same frustrations. I was using ARC in a project where I was presenting a camera using AV Foundation. After presenting and popping the view controller a few times, my app would receive a low memory warning, and subsequently crash. Instruments didn't help much either. I discovered the solution by experimenting:

Even though you are using ARC in your camera class, you can implement the dealloc method (just don't call super on dealloc).

- (void)dealloc {
    AVCaptureInput* input = [session.inputs objectAtIndex:0];
    [session removeInput:input];
    AVCaptureVideoDataOutput* output = [session.outputs objectAtIndex:0];
    [session removeOutput:output];  
    [session stopRunning];
}

This takes care of stopping the AVCaptureSession and ensuring it has no inputs or outputs still alive.

Coimbra answered 7/7, 2012 at 19:2 Comment(1)
Why just first input and output are removed? why not remove all inputs and outputs?Hocuspocus
C
13

I have encounter the same problem I have found this line is the main problem

[self.vImagePreview.layer addSublayer:captureVideoPreviewLayer];

Just remove the previewlayer from the super layer while deallocating and there is no memory issue. My deallocating function is as follow

 -(void)deallocSession
{
[captureVideoPreviewLayer removeFromSuperlayer];
for(AVCaptureInput *input1 in session.inputs) {
    [session removeInput:input1];
}

for(AVCaptureOutput *output1 in session.outputs) {
    [session removeOutput:output1];
}
[session stopRunning];
session=nil;
outputSettings=nil;
device=nil;
input=nil;
captureVideoPreviewLayer=nil;
stillImageOutput=nil;
self.vImagePreview=nil;

}

i called this function before popping and pushing any other view. It solved my issue.

Cleodell answered 11/12, 2013 at 6:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.