ios - Understanding NSAutoreleasePool -


i have app gets memory warning when using camera on iphone 4s. scale image before use it.

+ (uiimage*)simpleimagewithimage:(uiimage*)image scaledtosize:(cgsize)newsize {  // create graphics image context uigraphicsbeginimagecontext(newsize);  // tell old image draw in new context, desired // new size [image drawinrect:cgrectmake(0,0,newsize.width,newsize.height)];  // new image context uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext();  // end context uigraphicsendimagecontext();   // return new image. return newimage; } 

i read should use nsautoreleasepool here http://wiresareobsolete.com/2010/08/uiimagepickercontroller/

so modified code this:

+ (uiimage*)simpleimagewithimage:(uiimage*)image scaledtosize:(cgsize)newsize { //http://wiresareobsolete.com/2010/08/uiimagepickercontroller/  nsautoreleasepool *pool = [[nsautoreleasepool alloc] init];  // create graphics image context uigraphicsbeginimagecontext(newsize);  // tell old image draw in new context, desired // new size [image drawinrect:cgrectmake(0,0,newsize.width,newsize.height)];  // new image context uiimage* newimage = uigraphicsgetimagefromcurrentimagecontext();  // end context uigraphicsendimagecontext();  [newimage retain];  [pool release];  // return new image. return newimage; } 

the memory warning went away. did not call on separate thread. nsautoreleasepool doing? don't understand it.

can retain object in local nsautoreleasepool?

can use retained object after nsautoreleasepool has been released?

the important question: how specific usage of nsautoreleasepool memory footprint of app doesn't memory warnings?

first of all, should using @autoreleasepool blocks instead of nsautoreleasepool objects; latter pretty outdated. former nicer because automatically drains pool when leave scope of block without needing explicitly yourself.

an autorelease pool remembers things have been "autoreleased" while in scope, , releases them when pool drained. "autoreleasing" object releasing it, deferred; memory management perspective, takes strong reference have , "transfers" pool, pool has strong reference object , don't. in pre-arc, if method (whose name did not start new or copy) needs return object created during function, pretty had autoreleased. putting on pool, guarantees object alive received calling function. in code, uigraphicsgetimagefromcurrentimagecontext() return autoreleased object.

autorelease pools drain @ end of pool. objects in pool kept alive duration of pool (because "owned" pool). means if pool lasts long time , lots of things autoreleased on it, lots of objects prevented being deallocated, potentially bad.

for example, 1 run of function autoreleases 1 object (i.e. object returned uigraphicsgetimagefromcurrentimagecontext()). if run function 100,000 times in loop, 100,000 objects stay in memory, because put onto same pool hasn't had chance drain yet. however, if put level of pool each iteration of loop, drain @ end of each iteration, preventing objects building up.

as second piece of code, putting autorelease pool inside simpleimagewithimage: method catch autorelease uigraphicsgetimagefromcurrentimagecontext(). however, have problem because, in order return image object simpleimagewithimage: method itself, need autorelease it!

your method, written, violates memory management rules. method returns retained object, caller have remember release. however, caller doesn't know based on name. according naming conventions, methods can return retained instance names start alloc, retain, new, copy, , mutablecopy. method doesn't start of these, must return non-retained instance. since own object (by retaining it), must balancing release or autorelease. can't release because cause object potentially deallocated since there no other strong references it, can autorelease it.

but if going autorelease again, have accomplished nothing adding autorelease pool here, unless inside drawinrect: or inside uigraphicsgetimagefromcurrentimagecontext() autorelease lot of stuff.


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -