Question about when/when not to use AutoReleasePools?

Discussion in 'Mac Programming' started by czeluff, Nov 21, 2008.

  1. czeluff macrumors 6502

    Joined:
    Oct 23, 2006
    #1
    Hello to all,

    i'm still very new at learning Obj-C and have a question. First, take a look at the NSDictionary reference: http://developer.apple.com/document...s/NSDictionary_Class/Reference/Reference.html

    Scroll down to Tasks, and youll see two types of methods: "Creating" an NSDictionary and "Initializaing" an NSDictionary. Both have pretty similar methods. However, I know that the "creating" methods interact with your Autorelease pool, and so you don't have to "release" them; you can simply do [pool drain] at the end of the program.


    My question is, why not ALWAYS use the "create" methods? Why would I ever want to "initialize" the object, and have to worry about manually releasing that object separately from the NSAutoreleasePool?
     
  2. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    I don't know why Apple makes a distinction there. Both set of methods do the same thing, but the "creating" methods return an autoreleased object. Autoreleased objects get released when the autorelease pool is released, which happens at a later date. So if you want to keep around an object you created (for example, you create it in an init method and reference it later), use the "initializing" methods, or retain the result of the "creating" methods.
     
  3. SydneyDev macrumors 6502

    Joined:
    Sep 15, 2008
    #3
    If you have a long running program you would not want to wait until the end to release all the objects, it would take up too much memory. If it is a GUI type app you are doing, and you use one of the templates in Xcode, it will drain the pool at the end of each event loop, in which case yes it's usually easier to use the factory methods (as they are known) instead of the initializers.
     

Share This Page