• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: newbie question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: newbie question


  • Subject: Re: newbie question
  • From: Bill Bumgarner <email@hidden>
  • Date: Fri, 10 Nov 2006 08:46:07 -0800

On Nov 10, 2006, at 8:37 AM, Alan Smith 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.
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.

That isn't quite correct. In a Cocoa application, an autorelease pool is created and then drained during each pass through the main event loop. Or, more correctly, an autorelease pool is created and drained during each pass through a run loop, of which the main event loop is just the main thread's run loop.


So, no, autoreleased objects in Cocoa typically will *not* stick around until a program is quit.

I prefer: NSMutableDictionary *dict = [[NSMutableDictionary alloc] init];
// Lots of code
[dict release];


Over: NSMutableDictionary *dict = [NSMutableDictionary dictionary];

Keeping your memory clean makes your program faster and look better.

Don't do this until you use a tool like Shark or, even, sample to determine that load caused by autorelease pool activity is actually causing a performance problem in your app. While the above is relatively innocuous and falls pretty close to Cocoa design patterns (you are free to manage memory however you want within a single scope -- within a body of a method), you certainly don't want to diverge from standard Cocoa memory management patterns until you determine that it causes a real performance issue.


b.bum

_______________________________________________
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


References: 
 >newbie question (From: maximilian marcoll <email@hidden>)
 >Re: newbie question (From: "Alan Smith" <email@hidden>)

  • Prev by Date: Re: newbie question
  • Next by Date: Re: newbie question
  • Previous by thread: Re: newbie question
  • Next by thread: Re: newbie question
  • Index(es):
    • Date
    • Thread