3

The screenshot of Leak Profiling in Instruments Tool: https://i.stack.imgur.com/rthhI.png

I found my UIImage objects leaking using Instruments tool. Per Apple's documentation, the object returned from UIGraphicsGetImageFromCurrentImageContext should be autoreleased, I can also see "Autorelease" event when profiling (see the first 2 lines of history of my attached screenshot). However, it seems that the "autorelease" event takes no effect. Why?

EDIT:

Code attached, I use the below code to "mix" two UIImages, also, later on, I use a UIMutableDictionary to cache those UIImage I "mixed". And I'm quite sure that I've called [UIMutableDictionary removeAllObjects] to clear the cache, so those UIImages "should be cleaned"

+ (UIImage*) mixUIImage:(UIImage*)i1 :(UIImage*)i2 :(CGPoint)i1Offset :(CGPoint)i2Offset{
CGFloat width , height;
if (i1) {
    width = i1.size.width;
    height = i1.size.height;
}else if(i2){
    width = i2.size.width;
    height = i2.size.height;
}else{
    width = 1;
    height = 1;
}

// create a new bitmap image context
//
UIGraphicsBeginImageContextWithOptions(CGSizeMake(width, height), NO, i1.scale);

// get context
//
CGContextRef context = UIGraphicsGetCurrentContext();       

// push context to make it current 
// (need to do this manually because we are not drawing in a UIView)
//
UIGraphicsPushContext(context);                             

// drawing code comes here- look at CGContext reference
// for available operations
//
// this example draws the inputImage into the context
//
[i2 drawInRect:CGRectMake(i2Offset.x, i2Offset.y, width, height)];
[i1 drawInRect:CGRectMake(i1Offset.x, i1Offset.y, width, height)];


// pop context 
//
UIGraphicsPopContext();                             

// get a UIImage from the image context- enjoy!!!
//
UIImage *outputImage = UIGraphicsGetImageFromCurrentImageContext();

// clean up drawing environment
//
UIGraphicsEndImageContext();

return outputImage;

}

huxia
  • 492
  • 1
  • 3
  • 16
  • Please include the code where you are generating the image – jrturton Sep 16 '11 at 08:02
  • This all looks fine, though I don't think you need to push and pop the context. Are you sure there is a leak? Can you show the other code you use these image objects in. – jrturton Sep 16 '11 at 11:45
  • Possible duplicate of [UIGraphicsGetImageFromCurrentImageContext memory leak with previews](http://stackoverflow.com/questions/5121120/uigraphicsgetimagefromcurrentimagecontext-memory-leak-with-previews) – Cœur Dec 24 '16 at 05:48

1 Answers1

-1

I was getting a strange UIImage memory leak using a retained UIImage image from UIGraphicsGetImageFromCurrentImageContext(). I was calling it in a background thread (in response to a timer event). The problem turned out to be - as mentioned deep in the documentation by apple - "you should only call this function from the main thread of your application". Beware.

goelectric
  • 320
  • 3
  • 10
  • 1
    As of iOS 4, you are allowed to call `UIGraphicsGetImageFromCurrentImageContext()` from any thread, so this shouldn't be the issue anymore. [link](https://developer.apple.com/Library/ios/documentation/UIKit/Reference/UIKitFunctionReference/Reference/reference.html#//apple_ref/c/func/UIGraphicsGetImageFromCurrentImageContext) – pwightman Nov 25 '13 at 17:31