• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Why shouldn't I release?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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

  • Follow-Ups:
    • Re: Why shouldn't I release?
      • From: "stephen joseph butler" <email@hidden>
    • Re: Why shouldn't I release?
      • From: Andrew Farmer <email@hidden>
    • Re: Why shouldn't I release?
      • From: j o a r <email@hidden>
  • Prev by Date: deleting an outlet in IB
  • Next by Date: Re: Why shouldn't I release?
  • Previous by thread: deleting an outlet in IB
  • Next by thread: Re: Why shouldn't I release?
  • Index(es):
    • Date
    • Thread