Does ObjC 2.0 mean no worrying about FREE and DEALLOC?

Discussion in 'Mac Programming' started by zippyfly, Mar 26, 2008.

  1. macrumors regular

    Joined:
    Mar 22, 2008
    #1
    Does the garbage collection in 2.0 mean we no longer have to really worry about freeing memory to avoid leaks?

    Can we just create objects and just move on with the code, without worrying to keep track and free them?

    a.k.a. can we be lazy (read: efficiently focus on the program logic rather than housecleaning) and get away with it?

    Thanks.

    PS - sorry I meant "release" and "dealloc"
     
  2. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    Yes and no. You still need -finalize methods (the new equivalent of -dealloc) in cases where you have non-memory resources to free up (say, sockets), and you might need to be careful if you ever reference non-garbage-collected memory from garbage-collected memory (say, if you use a C library in your app). There are a few other caveats, but in general it's pretty simple.
     
  3. macrumors 6502a

    Joined:
    Nov 10, 2006
    #3
    Also the garbage collector only cleans up objects. If you just malloc a bunch of memory you'll have to clean it up yourself.
     
  4. macrumors 6502a

    ebel3003

    Joined:
    Jun 20, 2007
    Location:
    "The Google"
    #4
    A few question: With Objective-C 2.0, calling [object release] doesn't matter anymore? Even if it doesn't, it would still be more efficient to clean up after yourself, right? Also, Objective-C 2.0 is included with Xcode 3.0, correct?
     
  5. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    ObjC is part of the OS, not part of Xcode, but Xcode 3 does support compiling ObjC 2 programs. [object release] is just a "do nothing" with garbage collection turned on. If you want to give hints to the GC system you can use NSAutoreleasePool's -drain method, or the NSGarbageCollector class.
     
  6. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #6
    You can be lazy. You still can't afford to be stupid. An object is not garbage as long as there is a usable pointer to it somewhere. So it would be a really, really stupid move to make a list of all objects that you don't care about, and put every object that you don't care about into that list...
     
  7. thread starter macrumors regular

    Joined:
    Mar 22, 2008
    #7
    Hi - sorry I don't quite understand what you mean.

    Say, I allocate a new object in my MAIN.

    I then call some method inside that object, which creates its own objects, etc.

    The next time I call the method, the instance creates OTHER new objects (e.g., to replace instance variable values) but I do not release the first objects (which are now obsolete, although I did not track them, nor did I issue a release command to the instance variables to free themselves before being assigned the newly created objects).

    And I do this over and over and over.

    Will the garbage collector release these objects for me?

    (The instance variables no longer point to the underlying objects, evidently in this case)
     

Share This Page