• 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: Checkboxes and NSBox titles
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Checkboxes and NSBox titles


  • Subject: Re: Checkboxes and NSBox titles
  • From: Jim Correia <email@hidden>
  • Date: Wed, 13 Jun 2001 09:07:43 -0400

On Wednesday, June 13, 2001, at 05:57 AM, John Hvrnkvist wrote:

I prefer this form:
- (void) setMyVal: (id) newVal
{
[newVal retain];
[myVal release];
myVal =newVal;
}

It avoids autorelease, and as I see it is much better description of what you want to do.

I'm new to Cocoa and objective-c so pardon my ignorance :-), but this code seems to have a problem that the tutorials say you can avoid with usage of auto-release. Is the contract of foundation objects that if it has copy or create in its name, the caller must release, if it has get in its name, you can retain if you'd like to keep, but otherwise the result will remain valid for the local calling context? If so, it would make sense to have your objects behave similarly so you can have a consistent calling interface between your objects and core objects. Anyway, if you write set val as the above, and the caller does this:

id val = [someObject getVal];

[somObject setVal:newVal];

[val doSomething]; // but now val is a dangling pointer because val was released in setVal


So I guess there are two ways to solve it. Do an auto release in setVal, or in getVal instead of simply returning val, return [[val retain] autorelease];

So which is the preferred way to go?

How are the foundation objects implemented?

Jim


  • Follow-Ups:
    • Re: Checkboxes and NSBox titles
      • From: John Hörnkvist <email@hidden>
References: 
 >Re: Checkboxes and NSBox titles (From: John Hörnkvist <email@hidden>)

  • Prev by Date: Performance book for Cocoa?
  • Next by Date: Re: Checkboxes and NSBox titles
  • Previous by thread: Re: Checkboxes and NSBox titles
  • Next by thread: Re: Checkboxes and NSBox titles
  • Index(es):
    • Date
    • Thread