The following block of code bit me today.
Thinking about it.... I understand why... I broke the golden rule of memory management with objective-C (e.g., if you didn't alloc it or retain it, then you don't release it). The bug is in the fact with the code that is broken, the allocated object passed to lowercaseString is lost when lowercaseString creates a new string with autorelease, and then later in the code I released an object that I didn't allocate or retain.
I hope this helps someone.
Code:
// this code is broken
-(NSString*)getCodeForCharacter:(unichar)ch
{
NSString *key = [[[NSString alloc] initWithCharacters:&ch length:1] lowercaseString];
NSString *value = [code valueForKey:key];
[key release];
return value;
}
Code:
// this code is correct
-(NSString*)getCodeForCharacter:(unichar)ch
{
NSString *key = [[NSString alloc] initWithCharacters:&ch length:1];
NSString *value = [code valueForKey:[key lowercaseString]];
[key release];
return value;
}