ios - Why tiny memory leak in this func related to UIGraphicsBeginImageContext? -


i have simple function create uiimage special size :

- (uiimage*)imagewithsize:(cgsize) imsize {     uigraphicsbeginimagecontext( imsize );      float r = 0.5f;     float g = 0.5f;     float b = 0.5f;      cgcontextref cxt = uigraphicsgetcurrentcontext();     cgcontextsetrgbstrokecolor(cxt, r, g, b, 1.0);     cgcontextsetrgbfillcolor(cxt, r,g,b, 1.0);     cgcontextfillrect(cxt, cgrectmake(0.0, 0.0, imsize.width, imsize.height));      uiimage* retimg = uigraphicsgetimagefromcurrentimagecontext();      uigraphicsendimagecontext();      return retimg; } 

and call many times in way (i'm using arc):

for(int i=0;i<3000;i++) {     uiimage* im = [self imagewithsize:cgsizemake(256,192)]; } 

it receive memory warning several times, crash on iphone4 ;( wrong simple function?

this typical when using large loops no have local autorelease pools.

since function not return control main loop, hence giving os chance drain pool of autoreleased objects, memory occupation keeps growing indefinitely.

this way of using local autorelease pool , release not needed object @ each iteration in loop (although such "study case" possibly overkill):

for(int i=0;i<3000;i++) {     nsautoreleasepool *looppool = [[nsautoreleasepool alloc] init];     uiimage* im = [self imagewithsize:cgsizemake(256,192)];     [looppool release]; } 

or use newer syntax:

    @autoreleasepool {          ....     } 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -