Re: Yet another memory management question
Re: Yet another memory management question
- Subject: Re: Yet another memory management question
- From: Greg Titus <email@hidden>
- Date: Fri, 8 Sep 2006 09:00:20 -0700
Hi Bruce,
Joar has the answer to your memory management problem (replacing
selectorString with a new autoreleased string), but you are also
using a couple of extra string objects here that you really don't
need to. I'd rewrite your code as:
-(void)parseDataForVariable:(NSString *) variable
atSurface:(int)variableSurfaceNumber
withParameters:(NSString *)parameters
{
NSString *tempString;
NSString *selectorString;
SEL selectorForFunction;
tempString = [variable capitalizedString];
if ([tempString isEqualToString:@"Cv"])
tempString = @"Rd";
selectorString = [[@"set" stringByAppendingString:tempString]
stringByAppendingString:@"Color:"];
selectorForFunction = NSSelectorFromString(selectorString);
//See if aSurface responds to the selector
if (selectorForFunction != NULL && [aSurface
respondsToSelector:selectorForFunction]){
//it responds so now send the message
[[surfaceArray objectAtIndex:variableSurfaceNumber]
performSelector:selectorForFunction withObject:[NSColor redColor]];
}
}
Notice how the releases at the end are now unnecessary because you
aren't directly allocating any NSString objects any more, just using
@"" strings.
Hope this helps,
- Greg
On Sep 8, 2006, at 8:32 AM, Bruce Truax wrote:
I have run into a memory management question that I do not
understand. I
have the following method:
-(void)parseDataForVariable:(NSString *) variable
atSurface:(int)variableSurfaceNumber
withParameters:(NSString *)parameters
{
NSString *tempString;
NSString *selectorString;
SEL selectorForFunction;
tempString = [[NSString alloc] initWithString:[variable
capitalizedString]];
if ([tempString isEqualToString:@"Cv"]){
[tempString release];
tempString = [[NSString alloc] initWithString:@"Rd"];
}
selectorString = [[NSString alloc] initWithString:@"set"];
selectorString = [selectorString
stringByAppendingString:tempString];
selectorString = [selectorString
stringByAppendingString:@"Color:"];
selectorForFunction = NSSelectorFromString(selectorString);
//See if aSurface responds to the selector
if (!selectorForFunction == 0){
if ([aSurface respondsToSelector:selectorForFunction]){
//it responds so now send the message
[[surfaceArray objectAtIndex:variableSurfaceNumber]
performSelector:selectorForFunction
withObject:[NSColor redColor]];
}
}
[tempString release];
[selectorString autorelease];
}
As written this method works properly. If I change
[tempString release];
To
[tempString autorelease];
The program crashes when the popautoreleasepool function runs.
Also, if I try [selectorString release] the program will crash. I
would
have thought that either release or autorelease would be valid for
both of
these strings since I only use them locally.
--
____________________________________________________________
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
_______________________________________________
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