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: Daniel Child <email@hidden>
- Date: Tue, 29 May 2007 21:50:47 -0400
Thanks, Clark. This helps a lot, especially the "you make it you
'break' (release) it" part. I have renamed my methods as well.
On May 29, 2007, at 3:32 PM, Clark Cox wrote:
You take ownership of an object if you create it using a method whose
name begins with "alloc" or "new" or contains "copy" (for example,
alloc, newObject, or mutableCopy), or if you send it a retain message.
You are responsible for relinquishing ownership of objects you own
using release or autorelease. Any other time you receive an object,
you must not release it."
So, following the rules: (BTW, using newXXX as the name for a method
like this is generally a bad idea; because it conflicts with the rule
stated above, I've renamed them for you):
+(id)thing {
id newThg = [[self alloc] init];
/* I created it (by calling +alloc), so I am responsible for
releasing it */
return [newThg autorelease];
}
+(id)thingWithStg: (int)val {
id newThg = [Thing thing];
[newThg setInstVar: val];
/* I didn't create it (+newThing did) so I am not responsible for
releasing it */
return newThg;
}
_______________________________________________
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