[英]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.