Re: Which class methods return autoreleased objects?
Re: Which class methods return autoreleased objects?
- Subject: Re: Which class methods return autoreleased objects?
- From: Paul Sargent <email@hidden>
- Date: Sat, 11 Dec 2004 11:20:24 +0000
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] / 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:]? _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden