Why shouldn't I release?

  • 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
  • On Nov 30, 2007, at 6:54 PM, Ric L. wrote:

    > By my reckoning, stringWithFormat should create a string with a
    > retain count of 1.

    Why do you say that? Before you answer, refer to the memory management
    policies here:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concep
    ts/ObjectOwnership.html
    >

    Cheers,

    j o a r
  • On 30 Nov 07, at 18:54, Ric L. wrote:
    > 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.

    Read the Cocoa memory management rules [1]. "stringWithFormat" doesn't
    fit the criteria of the fundamental rule, so the object it returns
    isn't yours to release.

    [1]: http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/
    MemoryManagementRules.html
  • > CC: <cocoa-dev...>
    > From: <joar...>
    > To: <tekenen...>
    > Subject: Re: Why shouldn't I release?
    > Date: Fri, 30 Nov 2007 18:58:59 -0800
    >
    >
    > On Nov 30, 2007, at 6:54 PM, Ric L. wrote:
    >
    >> By my reckoning, stringWithFormat should create a string with a
    >> retain count of 1.
    >
    >
    > Why do you say that? Before you answer, refer to the memory management
    > policies here:
    >
    > <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concepts/ObjectOwnership.html

    Well, after I wrote the message, I added a line:

        long retainCount = [outputString retainCount];

    after the call to stringWithFormat and saw that it had a retain count of 1 - this would seem to settle the issue that I am correct on this point at least.

    However, thanks for the pointer to the site, I will take a close look at it.

    _________________________________________________________________
    Your smile counts. The more smiles you share, the more we donate.  Join in.
    www.windowslive.com/smile?ocid=TXT_TAGLM_Wave2_oprsmilewlhmtagline
  • The result of -[retainCount] is not necessarily equal to its retain
    count from your point of view. It might be retained by something else
    that's not immediately obvious. For instance, your outputString will
    be released by the autoreleasepool. So yes, the retain count may be
    higher than 0, but that still means it's not up to you to do something
    about that in this case.

    Good luck,
    Hank

    On Nov 30, 2007, at 10:06 PM, Ric L. wrote:

    >
    >
    >
    >
    >> CC: <cocoa-dev...>
    >> From: <joar...>
    >> To: <tekenen...>
    >> Subject: Re: Why shouldn't I release?
    >> Date: Fri, 30 Nov 2007 18:58:59 -0800
    >>
    >>
    >> On Nov 30, 2007, at 6:54 PM, Ric L. wrote:
    >>
    >>> By my reckoning, stringWithFormat should create a string with a
    >>> retain count of 1.
    >>
    >>
    >> Why do you say that? Before you answer, refer to the memory
    >> management
    >> policies here:
    >>
    >> <http://developer.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Concep
    ts/ObjectOwnership.html
    >
    > Well, after I wrote the message, I added a line:
    >
    > long retainCount = [outputString retainCount];
    >
    > after the call to stringWithFormat and saw that it had a retain
    > count of 1 - this would seem to settle the issue that I am correct
    > on this point at least.
    >
    > However, thanks for the pointer to the site, I will take a close
    > look at it.
    >
    >
    >
    > _________________________________________________________________
    > Your smile counts. The more smiles you share, the more we donate.
    > Join in.
    > www.windowslive.com/smile?ocid=TXT_TAGLM_Wave2_oprsmilewlhmtagline
    >

    Hank Heijink
    <hank.list...>
  • On Nov 30, 2007 8:54 PM, Ric L. <tekenen...> wrote:
    > 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.

    The general rule of thumb is that "release" balances "alloc", "copy",
    or "mutableCopy" (I think I'm forgetting one here). You didn't call
    any of those methods above, so you shouldn't call "release".
  • On Nov 30, 2007, at 10:06 PM, Ric L. wrote:

    >
    >
    >
    >
    >> CC: <cocoa-dev...>
    >> From: <joar...>
    >> To: <tekenen...>
    >> Subject: Re: Why shouldn't I release?
    >> Date: Fri, 30 Nov 2007 18:58:59 -0800
    >>
    >>
    >> On Nov 30, 2007, at 6:54 PM, Ric L. wrote:
    >>
    >>> By my reckoning, stringWithFormat should create a string with a
    >>> retain count of 1.
    >>
    >>
    >> Why do you say that? Before you answer, refer to the memory
    >> management
    >> policies here:
    >>
    >> <http://developer.apple.com/documentation/Cocoa/Conceptual/
    >> MemoryMgmt/Concepts/ObjectOwnership.html
    >
    > Well, after I wrote the message, I added a line:
    >
    > long retainCount = [outputString retainCount];
    >
    > after the call to stringWithFormat and saw that it had a retain
    > count of 1 - this would seem to settle the issue that I am correct
    > on this point at least.
    >
    > However, thanks for the pointer to the site, I will take a close
    > look at it.

    True, it has a retain count of 1--if it had a retain count less than
    1 it would have already been deallocated.  stringWithFormat
    autoreleases the string, so it can be released when the autorelease
    pool is flushed.  But, you've already released it by then (causing it
    already to be deallocated); and the deallocated object being released
    causes the crash.

    Frank

    > _________________________________________________________________
    > Your smile counts. The more smiles you share, the more we donate.
    > Join in.
    > www.windowslive.com/smile?
    > ocid=TXT_TAGLM_Wave2_oprsmilewlhmtagline______________________________
    > _________________
    >
    > Cocoa-dev mailing list (<Cocoa-dev...>)
    >
    > 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:
    > http://lists.apple.com/mailman/options/cocoa-dev/<weinstockf...>
    >
    > This email sent to <weinstockf...>

    ****************

    Frank Weinstock
    Interim Associate Dean for Academic Affairs
    Director of Graduate Studies
    College-Conservatory of Music
    University of Cincinnati
    <frank.weinstock...>

    ****************

    Frank Weinstock
    Interim Associate Dean for Academic Affairs
    Director of Graduate Studies
    College-Conservatory of Music
    University of Cincinnati
    <frank.weinstock...>
  • On Nov 30, 2007, at 10:06 PM, Ric L. wrote:

    > Well, after I wrote the message, I added a line:
    >
    > long retainCount = [outputString retainCount];
    >
    > after the call to stringWithFormat and saw that it had a retain
    > count of 1 - this would seem to settle the issue that I am correct
    > on this point at least.

      Just follow the memory management rules to which you were referred.
    The retain count is none of your business (because other background
    mechanisms may have an interest in your object(s) as well). Manage
    your memory properly and never mind the actual count.

    --
    I.S.
previous month december 2007 next month
MTWTFSS
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31            
Go to today