Re: memory mgmt in convenience and accessor methods
Re: memory mgmt in convenience and accessor methods
- Subject: Re: memory mgmt in convenience and accessor methods
- From: "Clark Cox" <email@hidden>
- Date: Tue, 29 May 2007 09:54:39 -0700
On 5/29/07, Daniel Child <email@hidden> wrote:
Hi All,
I am trying to build some basic test classes and am trying to learn
the appropriate setup for accessors and inits and convenience
methods. While various sources recommend having a "preferred init"
method for each class (the most complicated one), I am wondering if a
similar principle applies to "constructor" (convenience?) methods. Is
the following code ill-advised for any reason? I am in particular
wondering about the double autoreleasing.
+(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, 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.
--
Clark S. Cox III
email@hidden
_______________________________________________
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