0

I am manipulating an image with the following method and there is a memory leak at first line. The code is not ARC so I have to manually release the memory. How can I release the memory leaked in the first line of the following function?

-(UIImage*) manipulateImage :(UIImage *)image :(int)intType
{

    CIImage* inputImage = [[[CIImage alloc] initWithImage:image] autorelease]; //leak is here.
    CIFilter* filter = [CIFilter filterWithName:@"CIColorControls"];
    [filter setValue:inputImage forKey:kCIInputImageKey];

    [filter setValue:@(intType) forKey:kCIInputSaturationKey];

    CIImage* result = [filter valueForKey:kCIOutputImageKey];
    CIImage* returnImage = [result imageByCroppingToRect:[result extent]];
    return [[[UIImage alloc] initWithCGImage:returnImage.CGImage] autorelease];
}
Abdul Jamil
  • 363
  • 3
  • 13
  • If I use @autoreleasepool like Dave's answer here: https://stackoverflow.com/a/22132523/2866984 then the app crashes as the returned image is released before I can use it in other function. – Abdul Jamil Apr 08 '19 at 06:59
  • If you use an autoreleasepool you should not mark the returned uiimage to be autoreleased. release it when you dont need it anymore. – MartinM Apr 08 '19 at 09:47
  • @MartinM: You still have to `autorelease` objects within the pool block. If you don't they are not added to the autoreleasepool, and are not released when the pool drains. – Rengers Apr 08 '19 at 10:13
  • @wvteijlingen well thats the point, OP does not want to be the UIImage to be released when the pool drains (at the end of the function) – MartinM Apr 08 '19 at 10:16
  • You don't want to keep a reference to the image after the `manipulateImage` function returns, that would be dangerous memory management. The docs state: "If you own an object (...) you are responsible for releasing it (...).". The `manipulateImage` function creates the object, so it is also responsible for releasing it, by calling `release` or `autorelease` before the it returns. – Rengers Apr 08 '19 at 10:19
  • @wvteijlingen thanks. I have tried both options but still, I have high memory usage. I have also tried to release memory in the function where this returned image is used and now there are no leaks but mmap is using high memory. I think cache is not being cleared. Any ideas for that? – Abdul Jamil Apr 08 '19 at 14:34

0 Answers0