• 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: memory mgmt in convenience and accessor methods
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: memory mgmt in convenience and accessor methods


  • Subject: Re: memory mgmt in convenience and accessor methods
  • From: Uli Kusterer <email@hidden>
  • Date: Tue, 29 May 2007 23:04:50 +0200

On 29.05.2007, at 18:54, Clark Cox wrote:
On 5/29/07, Daniel Child <email@hidden> wrote:
+(id) newThing {
        id newThg = [[self alloc] init];
        return [newThg autorelease];
}

+(id) newThingWithStg: (int)val {
        id newThg = [Thing newThing];
        [newThg setInstVar: val];
        return [newThg autorelease];

You've just autoreleased newThg a second time. Don't do that :)

Also, it might be worth pointing out that traditionally, newXXX methods used to return a retained object, i.e. you shouldn't autorelease at all (only from the place where you're calling it). newXXX methods are pretty rare in Cocoa these days, but to prevent confusion if a NeXT old-timer should ever have to work on your code, you may want to avoid the "new" prefix, which used to be a shorthand for alloc/init back in the day.


However, for +(id) thing; and +(id) thingWithStg: (int)val; convenience factory methods, the newThing would autorelease. +thingWithStg: doesn't have to autorelease, because +thing is a convenience constructor that is already guaranteed to give an autoreleased object.

Also, while there are three memory management strategies recommended
for the accessors, I am wondering if any retains are needed if the
instance variables are primitive data types.

No, they aren't.

In other words:

-(void) setInstVar: (int) val {
        instVar = val; // OK without retains / releases ??
}

-(int) instVar {
        return instVar;
}

If the instance vars are primitive types, then retains/releases aren't even possible, much less required.

You only release/retain objects, that is, anything that is of type "id" or a subclass of that (NSObject and NSProxy are subclasses of "id", for the purposes of this discussion).


Cheers,
-- M. Uli Kusterer
http://www.zathras.de



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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


References: 
 >memory mgmt in convenience and accessor methods (From: Daniel Child <email@hidden>)
 >Re: memory mgmt in convenience and accessor methods (From: "Clark Cox" <email@hidden>)

  • Prev by Date: Re: Weird problem involving memory management and global variable
  • Next by Date: Re: Disabling items in NSTabView
  • Previous by thread: Re: memory mgmt in convenience and accessor methods
  • Next by thread: NSArray lookup, pointer comparison
  • Index(es):
    • Date
    • Thread