1

我正在嘗試對另一個.m文件中的圖像進行處理。目前以下是我的代碼。我有一個全局NSMutableArray存儲兩個UIImages並處理這兩個。每次用戶點擊一個按鈕,它都會將兩張圖片存儲到全局數組中,然後刪除這些元素。我使用ARC,因此我不需要釋放。使用ARC的內存泄漏GPUImage

@implementation 
NSMutableArray * imagesArray; 
ImageProcessor *imageProcessor; 
... 

- (void)viewDidLoad { 
imagesArray = [[NSMutableArray alloc] init]; 
imageProcessor = [[ImageProcessor alloc] init]; 
//some other code 
} 

-(UIImage*)processImages{//process images using GPUImage 
    UIImage *firstImg = [[imagesArray objectAtIndex:1] copy]; 
    UIImage *secImg = [[imagesArray objectAtIndex:0] copy]; 
    UIImage *processedImage = [imageProcessor flashSubtract:firstImg : secImg]; 
    UIImage *dividedImage = [imageProcessor referenceDivide:processedImage]; 
// [self uploadDropbox:UIImagePNGRepresentation(processedImage) : @"Output.png"];//try to save tiff files 
    //using ARC, no explicit memory releasing required 
    NSLog(@"image processed!"); 
    [imagesArray removeAllObjects]; 
    return dividedImage; 
} 

ImageProcessor.m:

#import "ImageProcessor.h" 

@interface ImageProcessor() 

@end 

@implementation ImageProcessor 

GPUImageSubtractBlendFilter *subFilter; 
GPUImageDivideBlendFilter* divFilter; 

-(id)init { 
    self = [super init]; 
    //initialize filters 
    subFilter = [[GPUImageSubtractBlendFilter alloc] init]; 
    divFilter = [[GPUImageDivideBlendFilter alloc] init]; 
    return self; 
} 

-(UIImage*)flashSubtract:(UIImage*) image1 : (UIImage*) image2{ 
    UIImage *processedImage; 
// @autoreleasepool { 

    //CAUSING MEMORY ISSUE 
    GPUImagePicture *img1 = [[GPUImagePicture alloc] initWithImage:image1];//image with flash 
    GPUImagePicture *img2 = [[GPUImagePicture alloc] initWithImage:image2];//image without flash 
    //MEMORY ISSUE END 

    [img1 addTarget:subFilter]; 
    [img2 addTarget:subFilter]; 

    [img1 processImage]; 
    [img2 processImage]; 
    [subFilter useNextFrameForImageCapture]; 
    processedImage = [subFilter imageFromCurrentFramebuffer]; 

// } 

    //consider modifications to filter possibly? 


    return processedImage; 
} 
@end 

我得到了內存泄漏問題,即後[imageProcessor flashSubtract]它不釋放內存。內存使用量持續增長,大約30張圖片後,應用程序崩潰。請讓我知道如果我做錯了什麼。任何幫助將非常感激。

+0

對不起,我正在測試這個__weak,真的只是用盡了很多選擇,因爲我對於目標c是相當新的。是的,我覺得使用nsmutable陣列可能與它有關 –

回答

2

首先,我建議通過靜態分析儀運行的代碼(Xcode的「產品」菜單上的「分析」,或按轉變 - 命令 - ),它可以識別問題有用在Objective-C代碼中。在繼續之前,請確保分析儀有乾淨的健康狀況。使用ARC,這裏可能沒有太多問題,但值得檢查一下,只是爲了確保。

其次,當您收到泄漏報告時,這不一定是造成泄漏的原因。它只是向您顯示泄漏對象最初創建的位置,因此您可以查看代碼並找出特定對象泄漏的原因。例如,我跑了通過「泄漏」的一個例子,它指示我該例程:

enter image description here

沒有那樣可怕的照明。很高興知道泄漏的內容,但我寧願找出對象泄漏的原因,而不僅僅是最初分配泄漏對象的位置。當我運行應用程序(不是通過樂器,而是在調試器中運行它),然後點擊「調試內存圖」按鈕,enter image description here,然後我可以點擊我應該在左面板,我現在可以看到什麼對象在保持強大的參考。在這種情況下,我可以看到我不小心建立了有力的參考週期:

enter image description here

有了這些信息,我可以追查其中這些強引用的成立並找出原因,他們仍然存在。或者,在這個例子中,我將追蹤爲什麼我有一個強大的參考週期,並找出哪些參考文獻需要是weak

+1

這真的很有幫助!我運行了構建分析器,並意識到我沒有正確地釋放我的核心圖像對象,如CGImageRef對象。看起來這些仍然需要使用特定的CFRelease功能發佈,即使啓用了ARC。非常感謝! –