Re: Release understanding help…
Re: Release understanding help…
- Subject: Re: Release understanding help…
- From: Thomas Davie <email@hidden>
- Date: Tue, 7 Dec 2004 11:55:46 +0000
you are reassigning a new NSString that is autoreleased by the
stringByAppendingString method and are leaking memory of the original
string. You could initialize validChars like the following to fix it:
NSString * validChars = [[NSString alloc] initWithFormat :
@"%@%@",@"0123456789mc/'\" ",[[NSUserDefaults standardUserDefaults]
objectForKey:NSDecimalSeparator]];
//and remove the following line :
//validChars = [validChars stringByAppendingString:[[NSUserDefaults
standardUserDefaults] objectForKey:NSDecimalSeparator]];
The alternative and possibly slightly neater and more cocoaish approach
is to autorelease validChars so that it doesn't leak:
NSString* validChars = [[[NSString alloc] initWithString:
@"0123456789mc/'\" "] autorelease];
validChars = [validChars stringByAppendingString:[[NSUserDefaults
standardUserDefaults] objectForKey:NSDecimalSeparator]];
You don't need release at the bottom of the program, because both of
the allocations are autoreleased.
Then of course you can just use the shorthand for the first statement:
NSString *validChars = @"0123456789mc/\" ";
And then you don't even need to give it a name:
validChars = [@"0123456789mc/\" "
stringByAppendingString:[[NSUserDefaults standardUserDefaults]
objectForKey:NSDecimalSeparator]];
This is of course a trade off though – bryon's approach is more
efficient, this is possibly slightly neater looking and clearer to the
programmer.
Bob
--
Computer Science is as much about computers as astronomy is about
telescopes -- Edsgar Dijkstra
_______________________________________________
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