On 30 Aug, 2010,at 11:33 AM, Dave DeLong <davedel...@me.com> 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 (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/aglee%40mac.com

This email sent to ag...@mac.com_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to