objective c - Class method that returns object and transfers ownership -
[this memory management clarification/question]
- i have class method returns nsmutablearray*.
- i want caller own , have call release.
- i don't want use autorelease because want freed memory caller done.
is ok long start name new or alloc, correct?
e.g.
+ (nsmutablearray*) newarrayfromdb { nsmutablearray* myarray = [[nsmutablearray alloc] init]; //stuff populate array return myarray; }
then caller call with:
nsmutablearray* test = [thatclass newarrayfromdb]; //use test [test release];
the static analyzer seems think ok...but wanted second opinion...
thanks feedback....
yes, you're doing correct - you're following rules in object ownership policy correctly.
the point might question i don't want use autorelease because want freed memory caller done
. default autorelease pool drained every time through main run loop, in cases memory released anyway. and, if caller needs force release sooner, can allocate/drain autorelease pool manually. of course, 1 approach create 2 convenience methods + newarrayfromdb:
, +arrayfromdb
; 1 autoreleases , 1 doesn't.
Comments
Post a Comment