ios - When via a class method get an object, why before return the value, the method can put the value into AutoReleasePool? -
e.g.
method1:
-(void)method1{ id array1 = [[nsmutablearray alloc] init];//now,retaincount of array1 1 }
method2:
-(void)method2{ id array2 = [nsmutablearray array]; //now,retaincount of array2 2 }
question 1:int method2,the implementation of class method +array not utilize [alloc [init]]? why did not homecoming value directly?and set value in autoreleasepoll?
question 2:which improve (faster & more efficient) utilize between 2 way? why?
both of these statements equivalent , sec 1 doesn't add together retain count of array1
. first method function assume ownership , subsequently responsible release calls later on, arc don't have worry these details.
in pre-arc days, values methods homecoming set in autorelease pools avoid premature deallocation , memory leaks. consider next cases:
-(nsarray *)emptyarray { // create homecoming value calls alloc , init. nsarray *returnarray = [[nsarray alloc] init]; homecoming returnarray; }
because there no phone call [returnarray release]; value returned method never deallocated , there memory leak. @ first may seem there no direct solution this.
-(nsarray *)emptyarray { nsarray *returnarray = [[nsarray alloc] init]; homecoming returnarray; [returnarray release]; // unreachable statement }
and
-(nsarray *)emptyarray { // create homecoming value calls alloc , init. nsarray *returnarray = [[nsarray alloc] init]; [returnarray release]; homecoming returnarray; // homecoming deallocated object }
so instead add together array autorelease pool released after time--long plenty retained whatever receiving homecoming value, still preventing memory leaks.
ios objective-c cocoa memory-management automatic-ref-counting
No comments:
Post a Comment