1

In ios a primary autorelease pool wraps around UIApplicationMain. If I don't manually set up other autorelease pools inside the app, does that mean that whenever I release an object in the app with autorelease, it will not actually be released until UIApplicationMain is done, the pool is drained, and the app terminates?

That's what seems to be the case and if it is it seems a very bad idea to use autorelease. All objects, even if just created for temporary reason, will stick around until the app ends, waisting a lot of memory.

For example, many factory methods I think use autorelease. Does everything created using a factory method only get released when the app finishes?

1
  • 1
    See also stackoverflow.com/questions/29350634/… for more on the top-level autorelease pool. It isn't technically needed anymore, but continues to be included for historical, compatibility, and consistency reasons.
    – Rob Napier
    Apr 24, 2015 at 20:33

1 Answer 1

5

Not at all!

UIApplicationMain creates the main run loop for your app. You can treat that as an infinite loop that handles your messages in current queue and continues its next iteration.

Autorelease pool drains all autoreleased objects at the end of every run loop iteration. That means, say your app handles a touch event on a button and reloads a table view in a single iteration of run loop. Then all the autoreleased objects created in the mean time would be drained after the iteration completes.

The run loop will still continue to run till the application runs and would continue to handle further events.

However, if you create separate thread in your app that would have its own run loop and consequentially it is your responsibility to create the autorelease pool for any new thread you spawn.

2
  • Your answer would be clearer if it explained that the main run loop has its own autorelease pool.
    – jlehr
    Apr 24, 2015 at 21:20
  • Oh, I see. Yes if the run loop has its own autorelease that I didn't know about that all makes sense. Thank you. Apr 24, 2015 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.