• 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: John Hörnkvist <email@hidden>
  • Date: Wed, 13 Jun 2001 15:43:09 +0200

On Wednesday, June 13, 2001, at 03:07 PM, Jim Correia wrote:

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

No, you should NOT count on that.

Try this:
main()
{
NSAutoreleasePool* pool=[[NSAutoreleasePool alloc] init];
NSMutableArray* myArray=[[NSMutableArray alloc] init];
NSString* someString=[[NSString alloc] initWithString:@"Hello"];
NSString* sameString=nil;

[myArray addObject:someString];
[someString release];
sameString=[myArray lastObject];
[myArray release];

NSLog(sameString);
[pool release];
}

If you want to keep a reference to an object you _always_ retain it, even if it's only for the scope of a method.

Using the auto release pool should be avoided; their use can blow up the footprint of a program immensely.

Rule #1 of autorelease pools:
Use autorelease only when it's impossible to use an immediate release.
(i.e. return [someObject autorelease]; and such)


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?

Neither.

Retain where you use it:

id val = [[someObject getVal] retain];
[somObject setVal:newVal];
[val doSomething];
[val release];

Regards,
John Hornkvist
--
ToastedMarshmallow, the perfect Cocoa companion
http://www.toastedmarshmallow.com


  • Follow-Ups:
    • Re: Checkboxes and NSBox titles
      • From: Jim Correia <email@hidden>
References: 
 >Re: Checkboxes and NSBox titles (From: Jim Correia <email@hidden>)

  • Prev by Date: Re: Checkboxes and NSBox titles
  • 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