1

다른 .m 파일의 이미지를 처리하려고합니다. 현재 다음은 내 코드입니다. 두 개의 UIImages를 저장하고 두 개를 처리하는 전역 NSMutableArray가 있습니다. 사용자가 버튼을 클릭 할 때마다 두 개의 그림을 전역 배열에 저장하여 처리 한 다음 요소를 제거합니다. 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으로 테스트를 진행 했었습니다. 객관적으로 새로운 것 때문에 옵션을 다 써 버리고있었습니다. 네, 그걸로 뭔가 할 수있는 nsmutable 배열을 사용하는 것 같아 –

답변

2

첫째, 정적 분석을 통해 코드를 실행하는 게 좋을 것 (- - 명령B변화를 엑스 코드의 "제품"메뉴의 "분석", 또는 누름) Objective-C 코드에서. 진행하기 전에 분석기의 건강 상태가 깨끗한 지 확인하십시오. ARC를 사용하면 여기에 문제가 너무 많지 않을 수도 있지만 확인하는 것이 좋습니다.

둘째, 누출 신고를 받았을 때 누설의 원인이되는 것은 아닙니다. 그것은 누출 된 객체가 원래 생성 된 곳을 보여주기 때문에 코드를 살펴보고 그 객체가 누설 된 이유를 파악할 수 있습니다. 예를 들어, 나는 "누출"을 통해 예제를 실행하며이 루틴에 나를 감독 : 정말 조명 아니라고

enter image description here

. 누수 된 것을 알면 좋지만, 누출 된 객체가 원래 할당 된 곳이 아닌 객체가 누설 된 이유를 알 수 있습니다.

그러나 어플라이언스를 통해 실행하지 않고 디버거에서 실행 한 후 "디버그 메모리 그래프"버튼 enter image description here을 두드리면 필자가 출시 한 개체를 탭 할 수있었습니다. 왼쪽 패널과 이제는 어떤 객체가 강력한 참조를 유지하는지 확인할 수 있습니다. 이 경우, 내가 실수로 설립했다 강한 참조주기 볼 수 있습니다 :이 정보

enter image description here

무장을, 나는이 강한 참조가 설립 된 곳 추적하고 그들은 여전히 ​​존재 이유를 알아낼 수 있습니다. 또는이 예에서 강한 참고주기가있는 이유를 추적하고 이러한 참조 중 어느 것이 weak 일 필요가 있는지 파악합니다.

+1

이것은 정말 도움이되었다! Build 분석기를 실행하고 CGImageRef 객체와 같은 핵심 이미지 객체를 제대로 릴리스하지 않는다는 것을 깨달았습니다. ARC가 활성화 된 상태에서도 특정 CFRelease 기능을 사용하여 릴리스해야합니다. 정말 고맙습니다! –

관련 문제