iOS - CMSampleBufferRef未从captureOutput发布:didOutputSampleBuffer:fromConnection

时间:2013-08-26 12:42:00

标签: iphone ios objective-c core-foundation

我正在使用代码从相机捕捉帧:

- (void)captureOutput:(AVCaptureOutput *)captureOutput
didOutputSampleBuffer:(CMSampleBufferRef)sampleBuffer
        :(AVCaptureConnection *)connection
{
    // Create a UIImage from the sample buffer data
    UIImage *image = [self imageFromSampleBuffer:sampleBuffer];

    if(delegate && [delegate respondsToSelector:@selector(captureManagerCapturedFrame:withFrameImage:withFrameBuffer:)]) {
        [delegate captureManagerCapturedFrame:self withFrameImage:image withFrameBuffer:sampleBuffer];
    }

}

我这样做是因为在委托方法captureManagerCapturedFrame:withFrameImage:withFrameBuffer:中我有一个标志,告诉应用程序使用返回的uiimage或返回的sampleBuffer

委托方法是:

- (void) captureManagerCapturedFrame:(AVCamCaptureManager *)captureManager
                      withFrameImage:(UIImage *)image
                     withFrameBuffer:(CMSampleBufferRef)frameBuffer {

    if(_screen1) {
        NSLog(@"Only display camera image\n");
    }
    else if(_screen2) {
        //Enable IR
        NSLog(@"Display AND Process camera image\n");
        [self imageReconigitionProcessFrame:frameBuffer];
    }
}

其中imageReconigitionProcessFrame:是:

-(void)imageReconigitionProcessFrame:(CMSampleBufferRef)frameBuffer {

    //CFRetain(frameBuffer);
    MSImage *qry = [[MSImage alloc] initWithBuffer:frameBuffer orientation:AVCaptureVideoOrientationPortrait]; //MEMORY LEAK HERE???
    qry = nil;
    //CFRelease(frameBuffer);
}

此代码有效。 但这是我的问题。当这些代码在工具中运行和分析时,我发现使用的overall bytes快速增加,但分配分析器似乎没有增加。也没有使用泄漏工具看到任何“泄漏”。但显然,每次调用imageReconigitionProcessFrame:时都会有快速的内存增益,并且应用程序会在几秒钟后崩溃。当我将frameBuffer设置为nil时,内存没有增加(或者当然我也没有帧缓冲区来进行任何处理)。

我尝试使用frameBufferCFRetain转移CFRelease的所有权(在上面的代码中已注明),但这些似乎也没有做任何事情。

有没有人知道我在这个功能中泄漏内存的地方??? 方法[[MSImage alloc] initWithBuffer:形成第三方SDK(Moodstocks,这是一个非常棒的图像识别SDK),它在演示中工作得很好,所以我不认为问题出在这个函数中。

1 个答案:

答案 0 :(得分:0)

首先,感谢您提及Moodstocks(我为他们工作):我们很高兴您发现我们的SDK很有用!

要回答您的问题,我猜您的代码确实包含泄漏:在imageReconigitionProcessFrame方法的末尾,您应该调用[qry release]。 Obj-C中的规则非常简单:每当您在对象上手动调用alloc时,它也应该手动释放!

那是BTW在Moodstocks SDK包装器中做了什么:如果你看一下[MSScannerSession session: didOutputSampleBuffer:]方法,你会发现我们在处理完{1}对象之后会手动释放它。

至于探查器为什么没有发现这种泄漏,我想这是因为默认情况下每隔10秒就会分析一次泄漏事件:在这种情况下,内存泄漏非常严重(1280x720帧,15 + FPS,如果你在iPhone 5上,持续10秒:至少130 MB泄露)代码必须在达到前10秒之前崩溃。

希望这有帮助!