• 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
Re: retain/release question about Apple docs
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: retain/release question about Apple docs


  • Subject: Re: retain/release question about Apple docs
  • From: Andy Lee <email@hidden>
  • Date: Sun, 5 Mar 2006 21:28:13 -0500

On Mar 5, 2006, at 8:43 PM, James W. Walker wrote:
In Apple's "Text System Overview", under "Building a Text Editor in 15 Minutes", there is the code:

- (void) setString: (NSAttributedString *) newValue {

    if (mString != newValue) {

        [newValue retain];

        if (mString) [mString release];

        mString = [newValue copy];

        [newValue release];

    }

}

I thought I understood the retain/release/autorelease business, but I can't see the need for the [newValue retain] and [newValue release] here.

Me neither, but here's a possible rationale. Instead of strings, imagine if mString was an object with an instance variable that held the last remaining reference to newValue. Then the [mString release] would free newValue, and newValue would be an invalid pointer, which would cause errors in the lines that follow. So this accessor pattern makes more sense in the more general case. Maybe the pattern was copied from some other code, or it's a pattern the author habitually uses.


What I don't understand is why a test for nil is applied to mString but not to newValue.

--Andy

_______________________________________________
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


References: 
 >retain/release question about Apple docs (From: "James W. Walker" <email@hidden>)

  • Prev by Date: Re: retain/release question about Apple docs
  • Next by Date: Re: Fastest Datastore?
  • Previous by thread: Re: retain/release question about Apple docs
  • Next by thread: Re: retain/release question about Apple docs
  • Index(es):
    • Date
    • Thread