Re: Retain/release question
Re: Retain/release question
- Subject: Re: Retain/release question
- From: Ricky Sharp <email@hidden>
- Date: Fri, 10 Dec 2004 17:56:07 -0600
On Dec 10, 2004, at 10:05 AM, Bruce Truax wrote:
I am still struggling with the retain/release concept. I hope someday
to
fully understand it. Most of the time I end up with the correct
pairing
because I poke around and look at the retain count and because most
times it
does not make much difference. I now have a case where I want to
create a 2
dimensional NSArray from a C array of floats and I have upwards of
200,000
elements in the array so I don't want to have a lot of improperly
retained
variables. Below I have included the source code to the function which
creates the array. Could someone provide me with the proper
retain/release
protocol? Should I set up an autorelease pool for this function?
When I call the function I the calling statement is:
[self setDisplayArray:[self buildArrayFromCArray:outputArray
ofSize:numberOfPoints
withXDimension:xSize
withYDimensiont:ySize]];
When I call this setter function and the display array is released I
want to
make sure that all of the row arrays are also released.
- (NSMutableArray *)buildArrayFromCArray:(float*)input
ofSize:(int)nPoints
withXDimension:(int)xDimension
withYDimensiont:(int) yDimension
Consider changing the method name to start with 'create' instead of
'build'.
{
NSMutableArray *theArray;
int column=0;
int row=0;
int i=0;
theArray = [[NSMutableArray alloc] initWithCapacity:xDimension];
This line is OK. Because your method creates a new array (hence the
recommended create in its name), you know that you'll later need to
explicitly release it.
If you were to change this method to be more of the convenience style
(e.g. arrayWithCArray), you would then want to add autorelease to this
line. You'd then have a convenience method that followed the pattern
of returning an autoreleased object. You'd thus have the
responsibility to retain if needed. But if you don't need it, you
wouldn't need to explicitly release it.
for (column = 0; column<xDimension; column++){
NSMutableArray *rowArray = [[NSMutableArray
alloc]initWithCapacity:yDimension];
for (row = 0;row<yDimension; row++){
NSNumber *point = [[NSNumber alloc]
initWithFloat:input[i]];
[rowArray addObject:point];
You should follow this line with:
[point release];
This is because the NSArray will retain added objects. So without the
release, each NSNumber would ultimately have a retain count of 2. When
you then release the array, each NSNumber would receive a single
release message which would just drop the retain count to 1. You'd
then be leaking all those NSNumbers.
Or, add an autorelease to the line where you alloc and init your
NSNumber.
You may then want to experiment with which approach can give you better
performance if your array is going to be large.
i++;
}
[theArray addObject:rowArray];
Same this as above. either explicitly release your rowArray object
after you add it to the array, or send it an autorelease message at the
time where you alloc & init it.
}
return theArray;
}
___________________________________________________________
Ricky A. Sharp mailto:email@hidden
Instant Interactive(tm) http://www.instantinteractive.com
_______________________________________________
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