Memory keep growing using AVCaptureSession

695 views Asked by At

Here is my code. I use such code in a clean new Xcode project, memory usage is steady. But in the application of my company, memory consumption keeps growing every about 7 seconds and finally the app crashes.

I simulate video buffer processing algorithm using

for(int volatile  i = 0; i < 100000000; i++){}

I set setAlwaysDiscardsLateVideoFrames to YES.

Strange things: 1. In a test application, memory keeps normal. 2. In application of my company, memory keeps growing on iPhone5s(8.3), but not on iPhone5(8.2). 3. Remove the for loop in didOutputSampleBuffer, my company's application also works well without memory rising.

So in conclusion, if I perform a time-consuming algorithm in buffer call-back function, and only in my company's application, memory keeps growing. On instrument with Activity Monitor I can see the memory growing. But I cannot see the allocation detail with Allocation instrument. It seems like CG raster data.

-(BOOL) setupAVCapture{
    session = [AVCaptureSession new];
    [session setSessionPreset:AVCaptureSessionPreset640x480];

    AVCaptureDevicePosition desiredPosition = AVCaptureDevicePositionFront;

    for (AVCaptureDevice *d in [AVCaptureDevice devicesWithMediaType:AVMediaTypeVideo]) {
        if ([d position] == desiredPosition) {
            [session beginConfiguration];
            AVCaptureDeviceInput *input = [AVCaptureDeviceInput deviceInputWithDevice:d error:nil];
            for (AVCaptureInput *oldInput in [ session inputs]) {
                [ session removeInput:oldInput];
            }
            [ session addInput:input];
            [ session commitConfiguration];
            break;
        }
    }

    // Make a video data output
    videoDataOutput = [AVCaptureVideoDataOutput new];

    // we want BGRA, both CoreGraphics and OpenGL work well with 'BGRA'
    NSDictionary *rgbOutputSettings = [NSDictionary dictionaryWithObject:
                                       [NSNumber numberWithInt:kCVPixelFormatType_32BGRA] forKey:(id)kCVPixelBufferPixelFormatTypeKey];
    [videoDataOutput setVideoSettings:rgbOutputSettings];
    [videoDataOutput setAlwaysDiscardsLateVideoFrames:YES]; // discard if the data output queue is blocked (as we process the still image)
    videoDataOutputQueue = dispatch_queue_create("MYFDVideoDataOutputQueue", DISPATCH_QUEUE_SERIAL);
    [videoDataOutput setSampleBufferDelegate:self queue:videoDataOutputQueue];

    if ( [session canAddOutput:videoDataOutput] )
        [session addOutput:videoDataOutput];
    [[videoDataOutput connectionWithMediaType:AVMediaTypeVideo] setEnabled:YES];

    [session startRunning];

    return YES;
}

#pragma mark delegate

- (void)captureOutput:(AVCaptureOutput *)captureOutput didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer fromConnection:(AVCaptureConnection *)connection
{
    CFRetain(sampleBuffer);
    for(int volatile  i = 0; i < 100000000; i++){}
    CFRelease(sampleBuffer);
}
0

There are 0 answers