Why shouldn't I release?
Why shouldn't I release?
- Subject: Why shouldn't I release?
- From: Ric L. <email@hidden>
- Date: Fri, 30 Nov 2007 21:54:06 -0500
- Importance: Normal
I am working through Cocoa Programming for Mac OS X and am at the end of Ch 4 - the challenge.
So, I have the following code:
- (IBAction)countLetters:(id)sender
{
NSString *theString = [fInputField stringValue];
long nLetters = [theString length];
NSString *outputString = NULL;
if ( nLetters <= 0 )
outputString = [NSString stringWithFormat:@"(no string to count)"];
else
outputString = [NSString stringWithFormat:@"%@ has %ld letters", theString, nLetters];
[fLetterCountField setStringValue:outputString];
[outputString release];
}
The problem may be clear to most, but not to me.
If I comment out the line:
[outputString release];
then everything apparently works correctly. With it in, the app either crashes or garbage ends up in the fLetterCountField.
This doesn't make any sense to me. By my reckoning, stringWithFormat should create a string with a retain count of 1. 'fLetterCountField setStringValue' should assign the string to the field and the field should retain the string. I should then be free to release it.
Of course, the documentation for 'setStringValue' does not claim that it retains the string.
Perhaps, I need to add the string to an autorelease pool?
Is it a mistake to release outputString? If so, why?
Or, is the problem elsewhere?
Thanks.
_________________________________________________________________
Share life as it happens with the new Windows Live.Download today it's FREE!
http://www.windowslive.com/share.html?ocid=TXT_TAGLM_Wave2_sharelife_112007_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden