Re: Style question
Re: Style question
- Subject: Re: Style question
- From: aglee <email@hidden>
- Date: Mon, 30 Aug 2010 21:45:11 +0000 (GMT)
On 30 Aug, 2010,at 11:33 AM, Dave DeLong <email@hidden> wrote:
My personal preference is the latter example. My general rule of thumb is that once I -release or -autorelease an object, I shouldn't interact with it anymore, since I have relinquished ownership of said object.
Definitely true once you release, but the whole purpose of autorelease is to allow you to interact with the object despite relinquishing ownership. For example, in Vincent's code he is returning the new autoreleased object precisely so the caller can use it.
For all the reasons already given, I actually think this is a case where there is a right answer and not so much a matter of personal preference. If you're going to autorelease, do so right away. I think this falls in the same category as "do the alloc and the init in the same statement."
--Andy
The only time I don't follow that guideline is when I'm working inside a method that has several different return routes, and cleaning up the appropriate objects at each point would just add to the complexity of the method (which could be argued is already too complex, but that's for another thread). In this case, I'll autorelease the appropriate objects immediately after initialization, thereby sparing me from having to clean stuff up in every single one of the return conditions.
Dave
On Aug 30, 2010, at 9:29 AM, Vincent Habchi wrote:
Hi everybody,
just an enquiry regarding coding style. What is considered best:
baz = [[[Foo alloc] init] autorelease];
…
return baz;
or
baz = [[Foo alloc] init];
…
return [baz autorelease];
?
Thanks!
Vincent
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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