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

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -