Clarification on autorelease

Discussion in 'iOS Programming' started by counciler, Feb 15, 2011.

  1. counciler macrumors newbie

    Joined:
    Jan 8, 2011
    #1
    I'm still a little fuzzy on autorelease because apparently methods will automatically throw stuff into it. I know that convenience methods like [UIImage imageWithContentsOfFile...] will add stuff to autorelease. So a couple of questions.

    1. The apple documentation states something about how an autorelease pool is automatically created and periodically released. So that's to say if I don't define an autorelease pool convenience methods could potentially cause a memory crash if I don't manage them correctly?

    2. Are there any other things I need to worry about being automatically added to a pool that I should look out for? All I know about are the ones that are stated similar to the previous stated example.

    3. Calling -drain or -release on the pool will have the same effect because we don't use garbage collection?
     
  2. hayesk macrumors 65816

    Joined:
    May 20, 2003
    #2
    Autorelease will release at the end of the run loop. While technically incorrect, you'll be ok assuming your autoreleased object will be released at the end of the method it is autoreleased in. Note, that's released, not dealloc'ed. If you add he object to an array, for example, the array will increase the retain count.

    You generally don't need to create your own autorelease pools unless you really need to release your objects early, or have them survive beyond the end of the run loop. Most tasks do not require you to make your own pool - you'll know when you need one.
     

Share This Page