2012-12-24 54 views
0

在圖像上應用GPUImage過濾器時,我遇到了一個奇怪的問題。我試圖對圖像應用不同的過濾器,但應用10-15個過濾器後,它會給我提示內存,然後崩潰。 這裏是代碼:GPUImage內存警告

sourcePicture = [[GPUImagePicture alloc] initWithImage:self.m_imageView.image smoothlyScaleOutput:YES]; 

      GPUImageBrightnessFilter *bright=[[GPUImageBrightnessFilter alloc]init]; 
      [bright setBrightness:0.4]; 
      GPUImageFilter *sepiaFilter = bright; 

      [sepiaFilter prepareForImageCapture]; 
      [sepiaFilter forceProcessingAtSize:CGSizeMake(640.0, 480.0)]; // This is now needed to make the filter run at the smaller output size 
      [sourcePicture addTarget:sepiaFilter]; 
      [sourcePicture processImage]; 
      UIImage *sep=[sepiaFilter imageFromCurrentlyProcessedOutputWithOrientation:3]; 

      self.m_imageView.image=sep; 
      [sourcePicture removeAllTargets]; 

如果有人經歷過同樣的問題,請建議。謝謝

+0

這個應用程序使用ARC嗎? – propstm

+0

不,這個程序沒有ARC。 – Superdev

回答

1

由於您沒有使用ARC,它看起來像你在幾個地方泄漏內存。通過不斷地分配,而沒有釋放您創建泄漏的價值。這是一篇關於內存管理的好文章。 https://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/MemoryMgmt.html

檢查以確保我已註釋的那些斑點正在正確釋放,然後再次檢查,如果您正在添加的15個過濾器中的每一個都有兩個潛在的泄漏點,那麼您將創建可能爲30個泄漏點。

編輯:我也爲你添加了兩個潛在的修復程序,但要確保你正確地管理你的內存,以確保你在其他地方沒有任何問題。

//--Potentially leaking here-- 
sourcePicture = [[GPUImagePicture alloc] initWithImage:self.m_imageView.image smoothlyScaleOutput:YES]; 

//--This may be leaking--  
GPUImageBrightnessFilter *bright=[[GPUImageBrightnessFilter alloc]init];    
[bright setBrightness:0.4]; 

GPUImageFilter *sepiaFilter = bright; 
//--Done using bright, release it; 
[bright release];       
[sepiaFilter prepareForImageCapture]; 
[sepiaFilter forceProcessingAtSize:CGSizeMake(640.0, 480.0)]; // This is now needed to make the filter run at the smaller output size 
[sourcePicture addTarget:sepiaFilter]; 
[sourcePicture processImage]; 
UIImage *sep=[sepiaFilter imageFromCurrentlyProcessedOutputWithOrientation:3]; 

self.m_imageView.image=sep; 
[sourcePicture removeAllTargets]; 
//--potential fix, release sourcePicture if we're done -- 
[sourcePicture release]; 
+3

請注意,如果您在狀態下釋放'bright',則會導致崩潰。因爲它是通過引用'sepiaFilter'分配的,所以後面的訪問將引用一個解除分配的對象。您至少需要在上述代碼中的'-addTarget:'用法之後移動該版本。 –