4

I'm experiencing some massive memory leaks that don't show up using the "leaks" instrument. I pop up a Modal View Controller and apply 2 CoreImage filters to 4 or 5 different images. Using Instruments I can see the memory jump up about 40-50 MB as these images are created, but even after I dismiss the Modal View Controller, I never get that memory back, and the application will crash after repeating this process 2 or 3 times. I'm happy for any advice you can provide because this is driving me absolutely crazy. Below is the method in question:

UIView *finalView = [[UIView alloc] initWithFrame:CGRectMake(1024, 0, 1792, 1345)];
UIImageView *templateImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 1792, 1345)];
templateImageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"%@.png",[theme objectForKey:@"template_background"]]];

//CI background Setup
NSString *filePath5 = [[NSBundle mainBundle] pathForResource:[theme objectForKey:@"template_background"] ofType:@"png"];
NSURL *fileNameAndPath5 = [NSURL fileURLWithPath:filePath5];

    @autoreleasepool {

        finalBackBeginImage = [CIImage imageWithContentsOfURL:fileNameAndPath5];
        finalBackImage = [CIFilter filterWithName:@"CIHueAdjust" keysAndValues:@"inputAngle", [NSNumber numberWithFloat:[[boothPrefs objectForKey:@"templateBackground_hue"] floatValue]*6.28], @"inputImage", finalBackBeginImage, nil].outputImage;
        finalBackImage = [CIFilter filterWithName:@"CIColorControls" keysAndValues:@"inputSaturation", [NSNumber numberWithFloat:([[boothPrefs objectForKey:@"templateBackground_saturation"] floatValue] * 5)], @"inputImage", finalBackImage, nil].outputImage;

        finalBackContent = [CIContext contextWithOptions:nil];
        CGImageRef cgimgFinalBack = 
        [finalBackContent createCGImage:finalBackImage fromRect:[finalBackImage extent]];
        UIImage *newFinalBackImg = [UIImage imageWithCGImage:cgimgFinalBack];
        [templateImageView setImage:newFinalBackImg];
        CGImageRelease(cgimgFinalBack);

    }

[finalView addSubview:templateImageView];
  • For reference, a few of the images being modified are 1024 x 768 pngs and a couple others are larger, maybe 1700 x 1300. I understand why it's taking a lot of memory to crunch these filters, I just don't understand why it's not being released. – Justin Gaynor Feb 22 '12 at 04:28
  • Are you removing your templateImageView before you are calling this function next time – Sunil Pandey Feb 22 '12 at 06:00
  • No, each image that get's added to finalView has it's own UIImageView. templateImageView is simply the first of the bunch. – Justin Gaynor Feb 22 '12 at 11:56
  • You are not the only one who's driven nearly mad by this issue! It really sucks, and I narrowed it down to using CIFilters... http://stackoverflow.com/questions/15771449/ipad-ios-memory-management-how-to-free-up-real-memory-used-by-uiimageviews – Alex Stone Apr 04 '13 at 04:13

1 Answers1

2

I've switched from using imageNamed to using imageWithData using the code below. In 5 minutes of testing (sorry, spent close to 12 hours on this issue now), I see that my real memory usage for the same operation is up to 50% lower (115mb versus up to 230 mb) and the mysterious "Push +80mb, pop -30mb" real memory issue appears to be solved.

I'm keeping my fingers crossed though.

//use images like this as base images for CIFilters
    NSData* imageData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:self.frameName ofType:nil]];
        ;
        UIImage* imageForFilter =[UIImage imageWithData: imageData];
Alex Stone
  • 46,408
  • 55
  • 231
  • 407