ios - ARC and autorelease -


autorelease used returned function object caller don't take ownership , callee release object in future.

however, arc capable count ownership of caller , release after use, is, can behavior smart pointer in c++. arc, can rid of autorelease because autorelease non-deterministic.

the reason ask question see returned object calls dealloc earlier in arc non-arc code. leads me think arc can behvior smart pointer , can make autorelease useless. true or possible? thing can think autorelease usefullness in multip-thread or network code because may not easier count ownership when object passing around.

thanks thoughts.

here new edit make thing clear:

with autorelease

+ (myclass*) myclass {     return [[[mycclass alloc] init] autorelease]; }  - dosomething {    myclass *obj = [myclass myclass]; } 

with arc:

+ (myclass*) myclass {     return [[mycclass alloc] init]; // no autorelease }  - dosomething {    myclass *obj = [myclass myclass];    // insert [obj release] } 

so, don't need autorelease.

autorelease mechanism still used arc, furthermore arc compiled-code designed interoperate seamlessly mrc compiled-code autorelease machinery around.

first, don't think in terms of reference counts in terms of ownership interest - long there declared ownership interest in object object lives, when there no ownership interest destroyed. in mrc declare ownership interest using retain, or creating new object; , relinquish ownership interest using release.

now when callee method creates object , wishes return caller callee going away needs relinquish ownership interest, , caller needs declare ownership interest or object may destroyed. there problem, callee finishes before caller receives object - when caller relinquishes ownership interest object may destroyed before caller has chance declare interest - not good.

two solutions used address this:

1) method declared transfer ownership interest in return value callee caller - model used init, copy, etc. methods. callee never notifies relinquishing ownership interest, , callee never declares ownership interest - agreement caller takes on ownership interest , responsibility of relinquishing later.

2) method declared return value in caller has no ownership interest, else maintain ownership interest in short period of time - until end of current run loop cycle. if caller wants use return value longer must declare own ownership interest, otherwise can rely on else having ownership interest , hence object staying around.

the question can "someone" maintains ownership interest? cannot callee method go away. enter "autorelease pool" - object can transfer ownership interest object stay around while. autorelease pool relinquish ownership interest in objects transferred in way when instructed - @ end of current run loop cycle.

now if above makes sense (i.e. if explained clearly), can see method (2) not required use method (1); but, , crucial but, under mrc lot more work programmer - every value received method comes ownership interest must managed , relinquished @ point - generate string output it? need relinquish interest in temporary string... (2) makes life lot easier.

one other hand computers fast idiots, , counting things , inserting code relinquish ownership interest on behalf of intelligent programmers suited to. arc doesn't need auto release pool. can make things easier , more efficient, , behind scenes arc optimises use - @ assembler output in xcode , you'll see calls routines name similar "retainautoreleasedreturnvalue"...

so right, not needed, still useful - under arc can (usually) forget exists.

hth more confuses!


Comments