Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Is [[[MYobj alloc] init] autorelease] kosher?

Is [[[MYobj alloc] init] autorelease] kosher?
Thread Tools
Mac Enthusiast
Join Date: Dec 2001
Status: Offline
Reply With Quote
Feb 5, 2003, 01:06 AM
 
I'm just learning Cocoa, so this is a simple question. If I have an object whose intended lifetime in the length of a method I'm writing, can I initialize it with code like in the subject?

I realize that the preferred style is to explicitly release the object when it is no longer needed, since I am not intending on returning it, but for various reasons I want to use a construct like the one I specified in some cases.
     
Mac Elite
Join Date: Sep 2000
Location: Tempe, AZ
Status: Offline
Reply With Quote
Feb 5, 2003, 02:04 AM
 
Yep, perfectly fine - do it all the time.

That said, if the object you're trying to create has a factory class method (like [NSArray array], for example), you can make your code a bit cleaner by using that instead. It's essentially the same thing as saying [[[NSArray alloc] init] autorelease].
Geekspiff - generating spiffdiddlee software since before you began paying attention.
     
Mac Elite
Join Date: Feb 2001
Location: Vancouver, WA
Status: Offline
Reply With Quote
Feb 5, 2003, 03:31 AM
 
Well, I'm not a rabbi, but I think you're safe.

Some ObjC conventions vary from person to person and case to case. Generally, this pattern is okay... we use it around here fairly often. Though it's probably best to make changes in object ownership explicit. For example, if you're creating an autoreleased object just so you can "transform" it, like with NSString and its various -stringByDoingSuchAndSuch methods, autoreleasing at the outset is probably good. But if you're, say, creating an NSMenu and then attaching it to an NSPopUpButton, it'll probably make your code clearer to others if you release or autorelease the object only once you hand it off to its new owner. But that's just my $1/50.
Rick Roe
icons.cx | weblog
     
Professional Poster
Join Date: Apr 2001
Location: Long Beach, CA
Status: Offline
Reply With Quote
Feb 5, 2003, 10:53 PM
 
The only thing that came to my mind is that anything that has been autoreleased is deallocated the next time through the event loop. I started thinking that if you had a multithreaded application that this might cause some problems... but then I realized that each thread has an autorelease pool. So, assuming that this works the way I think it would work, you should be fine even in this situation. As would naturally follow then would be that readability would be the only issue.

I may have to start using this method too.

ACSA 10.4/10.3, ACTC 10.3, ACHDS 10.3
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 08:45 PM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2