Re: Release understanding help…
Re: Release understanding help…
- Subject: Re: Release understanding help…
- From: Byron Wright <email@hidden>
- Date: Mon, 6 Dec 2004 18:17:07 -0800
the problem line is here:
validChars = [validChars stringByAppendingString:[[NSUserDefaults
standardUserDefaults] objectForKey:NSDecimalSeparator]];
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 same applies for disallowedCharacters. It's an autoreleased object
already so releasing it isn't safe. The same also applies for any
"static"/ class methods. You only release an object you have allocated
or retained .
not sure what the "#if 0 ", is for but it doesn't look very safe. I
would remove it :)
It would be a good idea to read over the following documentation, or
again with you have already :
http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/
RuntimeOverview/chapter_4_section_2.html. Memory Management is
fundamental to Cocoa/Objective-C and knowing it well will save you
hours / days/ weeks of headaches.
On Dec 6, 2004, at 5:35 PM, Mark Dawson wrote:
Another set of newbie questions…
I've created a new custom formatter using found code pieces
(1) When I try to do a release on the "validChars", I get a crash
(msg_objc). Shouldn't I be releasing it? I DID create it with an
alloc…
(2) The "validChars = [validChars " is obviously valid Obj-C, but I'm
not sure "where" the original string goes, as it isn't mutable. Does
the compiler take care of destroying the old string, or is my problem
that the assignment creates a string that doesn't have to be released
(because it returns a static value or autoreleased?), but leaks the
original string?
(3) disallowedCharacters also doesn't have to be released because its
getting a static value or autoreleased?
Thanks!
- (BOOL)isPartialStringValid:(NSString *)partialString
newEditingString:(NSString **)newString errorDescription:(NSString
**)error
{
NSRange foundRange;
// allow numbers, decimal point, ", ', m, c
NSString* validChars = [[NSString alloc] initWithString:
@"0123456789mc/'\" "];
validChars = [validChars stringByAppendingString:[[NSUserDefaults
standardUserDefaults] objectForKey:NSDecimalSeparator]];
NSCharacterSet *disallowedCharacters = [[NSCharacterSet
characterSetWithCharactersInString:validChars] invertedSet];
foundRange = [partialString
rangeOfCharacterFromSet:disallowedCharacters];
if(foundRange.location != NSNotFound)
{
*error = @"Illegal characters found in input";
NSBeep();
return(NO);
}
if ([partialString length] > 0)
*newString = partialString;
#if 0
[validChars release];
[disallowedCharacters release];
#endif
return(YES);
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
email@hidden
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