PDA

View Full Version : Does ObjC 2.0 mean no worrying about FREE and DEALLOC?




zippyfly
Mar 26, 2008, 10:50 PM
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"



Catfish_Man
Mar 27, 2008, 12:47 AM
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.

aLoC
Mar 27, 2008, 01:00 AM
Also the garbage collector only cleans up objects. If you just malloc a bunch of memory you'll have to clean it up yourself.

ebel3003
Mar 27, 2008, 01:11 AM
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?

Catfish_Man
Mar 27, 2008, 01:28 AM
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?

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.

gnasher729
Mar 27, 2008, 06:18 AM
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"

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...

zippyfly
Mar 27, 2008, 07:21 AM
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)