[英]Memory leak using ARC GPUImage
我正在嘗試對另一個.m文件中的圖像進行處理。 目前以下是我的代碼。 我有一個全局NSMutableArray來存儲兩個UIImage並處理這兩個。 每次用戶單擊按鈕時,都會拍攝兩張圖片,將它們存儲在全局數組中,然后處理它們,然后刪除元素。 我正在使用ARC,因此我不需要釋放。
@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張照片后,該應用程序崩潰了。 如果我做錯了事,請告訴我。 任何幫助將不勝感激。
首先,建議您通過靜態分析器(Xcode的“產品”菜單上的“分析”,或按shift- 命令 -B )運行代碼,這對於識別Objective-C代碼中的問題很有用。 在繼續操作之前,請確保從分析儀獲得干凈的健康單。 使用ARC,這里可能不會出現太多問題,但是值得確認一下。
其次,當您收到泄漏報告時,不一定是導致泄漏的原因。 它只是向您顯示泄漏對象的原始創建位置,因此您可以遍歷代碼並找出為什么該特定對象泄漏的原因。 例如,我通過“泄漏”運行了一個示例,它將我引向了該例程:
這並不是非常有啟發性的。 很高興知道泄漏了什么,但是我寧願找出原因,為什么泄漏對象,而不僅僅是泄漏對象最初分配的位置。
但是,當我運行該應用程序(不是通過Instruments運行,而是在調試器中運行)並點擊“調試內存圖”按鈕時, ,然后可以點擊應該在左側面板中釋放的對象,現在可以看到哪個對象正在維護強引用。 在這種情況下,我可以看到偶然建立的強大參考周期:
有了這些信息,我就可以找到這些強引用的建立位置,並弄清楚為什么它們仍然存在。 或者,在這個示例中,我將追蹤為什么我擁有一個強大的參考周期,並找出其中哪些參考需要是weak
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.