Memory Management question
Memory Management question
- Subject: Memory Management question
- From: Bruce Truax <email@hidden>
- Date: Sat, 25 Mar 2006 08:16:33 -0500
- Thread-topic: Memory Management question
I have a memory management question. A number of my objects have methods
which return string representations of the objects. These objects are
contained in large arrays and I often iterate through the arrays obtaining
the string representations of all of the objects. I want to make sure that
I properly manage the memory of these string representations so that I do
not have any leaks. There are two different ways I could write the code for
stringRepresentation. I believe that they are equivalent but I want to make
sure that they both work.
IMPLEMENTATION 1
- (NSString *)stringRepresentation
{
NSString *returnString;
returnString = [NSString stringWithFormat:@"%@ %@, %e %e %e %e %e\n",
[self command],
[self qualifier],
[[self parameter1] floatValue],
[[self parameter2] floatValue],
[[self parameter3] floatValue],
[[self parameter4] floatValue],
[[self parameter5] floatValue]];
return returnString;
}
IMPLEMENTATION 2
- (NSString *)stringRepresentation
{
NSString *returnString;
returnString = [[NSString alloc initWithFormat:@"%@ %@, %e %e %e %e
%e\n",
[self command],
[self qualifier],
[[self parameter1] floatValue],
[[self parameter2] floatValue],
[[self parameter3] floatValue],
[[self parameter4] floatValue],
[[self parameter5] floatValue]];
return [returnString autorelease];
}
If I understand the memory management documentation correctly, both of these
methods return an autoreleased NSString. Is this correct?
I can then process these strings as follows:
NSString * commandString
NSEnumerator * enumerator
enumerator = [extraDataArray objectEnumerator];
while (extraDataItem = [enumerator nextObject]){
commandString = [extraDataItem stringRepresentation];
[NSApp sendAction:@selector(processCommand:) to:nil
from:commandString];
}
I should never have to release commandString because it is autoreleased by
stringRepresentation. Because commandString is autoreleased I do not retain
it before sending it to NSApp with the sendAction command.
Am I doing this correctly?
One last question. If extraDataArray is large should I have a local
autorelease pool which I setup just outside this loop?
Thank you all for your advice.
Bruce
--
____________________________________________________________
Bruce E. Truax email: email@hidden
Optical Engineering Consultant
Diffraction Limited Design LLC
388 Wedgewood Road voice: 860-276-0450
Southington, CT 06489 fax: 860-620-9026
http://www.dld-llc.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