Skip navigation.
 
mlRe: Which class methods return autoreleased objects?
FROM : Paul Sargent
DATE : Sat Dec 11 12:20:24 2004

On 6 Dec 2004, at 23:16, Ricky Sharp wrote:
>
> Also remember that when writing your own class methods that create
> instances, they too should always return them autoreleased.  This will
> follow the proper pattern so that all objects can play nicely
> together.


So, here's a snippet of my code which I'm not sure what the rules
should be on it. It takes a NSData as input (we've been doing some
signal processing on the data), and converts to to an NSArray of
NSArrays to be used by the UI.

-(NSArray *)arrayRepresentationOf:(NSData *)aCurve {
   int i;
   unsigned int curveSize  = [aCurve length] / sizeof(float);
   
   NSArray        * coord;
   float          * curveBuffer = (float *)[aCurve bytes];
   NSMutableArray * curveArray  = [NSMutableArray
arrayWithCapacity:curveSize];
   
   for (i = 0; i < curveSize; ++i) {
       coord = [NSArray arrayWithObjects:[NSNumber numberWithInt:i],
                                         [NSNumber
numberWithFloat:curveBuffer[i]],
                                         Nil];
       [curveArray addObject:coord];
   }
   
   NSArray * returnArray = [NSArray arrayWithArray:curveArray];
   
   return returnArray;
}

Now here's how I currently understand things. Please correct me if I'm
wrong.

returnArray is an autoRelease'd object.
There's no local autoRelease pool, so it will survive the end of the
function call.
It will have been created in the parent's autoReleasePool scope, as
will all the local objects.
Therefore it's suitable for passing back.

Have I got my ideas straight. I've just spent ages tracking down a
autorelease related bug, so it's quite possible I've got a flaw in my
understanding.

Thanks

Paul

P.S. Is there a better way to convert a mutable array to an immutable
array than [NSArray arrayWithArray:]?

Related mailsAuthorDate
mlWhich class methods return autoreleased objects? Jeremy French Dec 6, 22:36
mlRe: Which class methods return autoreleased objects? Nick Zitzmann Dec 7, 00:15
mlRe: Which class methods return autoreleased objects? Ricky Sharp Dec 7, 00:16
mlRe: Which class methods return autoreleased objects? Gwynne Dec 7, 00:59
mlRe: Which class methods return autoreleased objects? M. Uli Kusterer Dec 7, 05:00
mlRe: Which class methods return autoreleased objects? Charlton Wilbur Dec 7, 05:43
mlRe: Which class methods return autoreleased objects? Scott Anguish Dec 7, 06:30
mlRe: Which class methods return autoreleased objects? Paul Sargent Dec 11, 12:20
mlRe: Which class methods return autoreleased objects? Clark Cox Dec 11, 13:39