![](/img/trans.png)
[英]captureOutput:didOutputSampleBuffer:fromConnection Performance Issues
[英]iOS - CMSampleBufferRef is not being released from captureOutput:didOutputSampleBuffer:fromConnection
我正在使用代码从相机捕获帧:
- (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
,内存不会增加(或者我当然也没有用于执行任何处理的帧缓冲区)。
我尝试使用CFRetain
和CFRelease
(在上面的代码中作了注释)转移frameBuffer
所有权,但是这些似乎也不起作用。
有谁知道我可能在此函数中泄漏内存??? [[MSImage alloc] initWithBuffer:
是由第三方SDK( Moodstocks ,这是一个很棒的图像识别SDK)组成的,并且在他们的演示中效果很好,所以我认为问题不在于此函数内。
首先,感谢您提到Moodstock(我为他们工作):我们很高兴您发现我们的SDK有用!
要回答您的问题,我想您的代码确实包含一个泄漏:在imageReconigitionProcessFrame
方法的末尾,您应该调用[qry release]
。 Obj-C中的规则非常简单:每当您在对象上手动调用alloc
时,也应该手动释放它!
顺便说一句,这是在Moodstocks SDK包装器中完成的:如果您查看[MSScannerSession session: didOutputSampleBuffer:]
方法,您会看到我们在处理完MSImage
对象后确实手动释放了它。
至于探查器为什么找不到此泄漏的原因,我猜这是由于默认情况下每10秒会分析一次泄漏:在这种情况下,内存泄漏非常重(1280x720帧,如果您的帧速率为15+ FPS在iPhone 5上运行10秒钟:至少泄漏130 MB),代码必须在到达前10秒钟之前崩溃。
希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.