I’m refactoring my OS X application for ARC. Opening the main.m file, I was sure I would find the Autorelease Pool instantiation and drain (like iOS projects) but to my big surprise it wasn’t there.
So my first question is:
- Where is the main Autorelease Pool?
My next question is:
- If the main Autorelease Pool isn’t created, do I need to create it? Or are autoreleased objects freed in some automagical way?
Your main.m file should have the following call:
NSApplicationMain()is responsible for creating the application, i.e., an instance ofNSApplication, which in turn is responsible for creating autorelease pools:This means that, in the general case, you shouldn’t have to worry about creating autorelease pools since
NSApplicationalready does that in both the initialisation and in the event loop. There are situations where creating your own autorelease pools might be necessary/desirable, e.g. a method that has a loop that creates many autoreleased objects. In this case, it’s a good idea to have an autorelease pool for each loop iteration.