4

I have written the following code snippet to take screen snapshot:

UIGraphicsBeginImageContext(animationView.frame.size);
[[window layer] renderInContext:UIGraphicsGetCurrentContext()];
UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

But, UIGraphicsGetImageFromCurrentImageContext seems to be leaking. Is this correct?

In Instruments I could not get the exact leak point. In activity monitor I observed that when I switch to the UI that executes the above code snippet memory increments by some MB. After this point it never decreases. Does UIGraphicsGetImageFromCurrentImageContext has memory leak? How do I solve this?

Edit

Instruments analysis Activity Monitor: shows the memory hike when this line of code is executed; never decreases even after releasing screenshot (UIImage)

Leaks and allocation, Heap Snapshot: Does not show any leak OR this allocation.

halfer
  • 19,824
  • 17
  • 99
  • 186
spd
  • 2,114
  • 1
  • 29
  • 54
  • 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:43

2 Answers2

-1

CGContextRef context = UIGraphicsGetCurrentContext();

/* you code */

CGContextRelease(context);

clear the context when done

Ruibin.Chow
  • 83
  • 1
  • 6
-1

You have just created a UIImage with data for your animationView (which could be some MB). Perhaps you should wrap this functionality in an autorelease pool.

 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];     
    UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext();
    //[screenshot retain]; //If you want precise control over when it is released and you will use it later.
    [pool release];
cdasher
  • 3,083
  • 1
  • 19
  • 20
  • I have not retained 'screenshot'. Do I need to release it then? – spd Aug 25 '11 at 13:39
  • 1
    Actually I guess UIGraphicsGetImageFromCurrentImageContext is autoreleased. Try wrapping it in an autorelease pool. – cdasher Aug 25 '11 at 13:47
  • I have tried this. But, of no use. When I created screenshot with some dummy image using the call [[UIImage alloc] initWithContentsOfFile:], real memory was decreased in Activity Monitor after its release. – spd Aug 25 '11 at 14:02