Re: newbie question
Re: newbie question
- Subject: Re: newbie question
- From: "Stephen Deken" <email@hidden>
- Date: Fri, 10 Nov 2006 11:02:28 -0600
On 11/10/06, Alan Smith <email@hidden> wrote:
One thing I didn't realize for quite some time when I was first
learning how to use the Cocoa frameworks is that all methods that have
the plus before them, should, return an autoreleased instance.
No, no, no! +[alloc] famously does not return an autoreleased
instance. The 'methods with a plus' are just class methods, and they
are free to return whatever they'd like, autoreleased or not.
Autoreleased objects are not something you should use a lot of because
they don't get released until your program quits, or sooner if you're
controlling the pool and specifically release it.
This is misleading. AppKit creates and releases autorelease pools all
the time -- each pass through the runloop for sure and probably more
often than that in practice. As long as you don't start creating
thousands upon thousands of objects in a single method, you're
probably ok. But you do *not* have to specifically control and
release the pool in order to use autoreleased objects, and
autoreleased objects do *not* persist until program shutdown in most
cases.
I prefer: NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; ... [dict release];
Over: NSMutableDictionary *dict = [NSMutableDictionary dictionary];
Keeping your memory clean makes your program faster and look better.
Agreed, with caveats -- using [dictionaryWith...] is helpful here:
NSArray *arr = [[NSArray alloc] initWithObjects:
[NSDictionary dictionaryWith...],
[NSDictionary dictionaryWith...],
[NSDictionary dictionaryWith...],
[NSDictionary dictionaryWith...],
nil];
Using -[dictionaryWith...] and letting the autorelease pool release
them (harmlessly, since they are retained by the array) makes the code
easier to read, and the impact on execution time is negligible.
Stephen Deken
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden