Iphone iOS-CMSampleBufferRef未从captureOutput:didOutputSampleBuffer:fromConnection中释放
我正在使用以下代码从相机捕获帧:Iphone iOS-CMSampleBufferRef未从captureOutput:didOutputSampleBuffer:fromConnection中释放,iphone,ios,objective-c,core-foundation,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
- (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];
}
}
其中,ImageReconficationProcessFrame:
是:
-(void)imageReconigitionProcessFrame:(CMSampleBufferRef)frameBuffer {
//CFRetain(frameBuffer);
MSImage *qry = [[MSImage alloc] initWithBuffer:frameBuffer orientation:AVCaptureVideoOrientationPortrait]; //MEMORY LEAK HERE???
qry = nil;
//CFRelease(frameBuffer);
}
该代码有效地工作但这是我的问题。在仪器中运行和分析此代码时,我看到使用的总字节数迅速增加,但分配分析器似乎没有增加。也不会使用泄漏工具看到任何“泄漏”。但是很明显,每次调用ImageReconficationProcessFrame:
并且应用程序在几秒钟后崩溃时,内存都会快速增加。当我将frameBuffer
设置为nil
时,内存没有增加(当然,我也没有帧缓冲区来进行任何处理)
我曾尝试使用CFRetain
和CFRelease
(在上面的代码中注释掉)转移frameBuffer
的所有权,但它们似乎也没有任何作用
有人知道我在这个函数中的内存泄漏在哪里吗???
方法[[MSImage alloc]initWithBuffer:
是一个第三方SDK(,这是一个很棒的图像识别SDK),在他们的演示中工作得很好,所以我认为问题不在这个函数内部。首先,感谢您提到MoodStock(我为他们工作):我们很高兴您发现我们的SDK很有用
为了回答您的问题,我想您的代码确实包含一个漏洞:在ImageRecognitionProcessFrame
方法的末尾,您应该调用[qry release]
。Obj-C中的规则非常简单:无论何时手动调用对象上的alloc
,都应该手动释放它
顺便说一句,这就是包装中所做的:如果您查看[MSScannerSession session:didOutputSampleBuffer:
方法,您将看到我们在处理MSImage
对象后手动释放它
至于为什么探查器找不到这个漏洞,我想这是因为默认情况下每10秒分析一次漏洞:在这种情况下,内存泄漏非常严重(1280x720帧,如果你在iPhone 5上,则为15+FPS,持续10秒:至少130 MB泄漏),代码必须在到达前10秒之前崩溃
希望这有帮助!我的项目已启用ARC,因此添加[qry release]
语句是不可行的。我已尝试将[[MSImage alloc]initWithBuffer:
直接添加到captureOutput:didOutputSampleBuffer:fromConnection
方法中(而不是通过委托类传递缓冲区,同样的问题仍然存在。好吧,我不熟悉ARC不幸的是…我通常是这里的Android开发人员:D。尽管如此,我会尝试检查[MSImage dealloc]
方法被调用:如果是,那么导致泄漏的对象不是qry
,而是其他东西…[MSImage dealloc]
实际上正在被调用。这是我认为它与帧缓冲区有关的另一个原因,而不是与MSImage创建有关。我仍然不确定这是否是Moodstocks问题,但我正在向help.Moodstocks.com发送一封电子邮件,其中包含一个复制该问题的演示应用程序。演示位于:if you Profile in instruments(通过分配),您应该会看到“总字节”值上升到超过gb,然后会崩溃。该值在您的环境中稳定吗?您可以给我发电子邮件:brett.spurrier(at)gmail(dot)com。也许我们可以找出对其他SO读者不重要的细节。我感谢您的帮助!