Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

zippyfly

macrumors regular
Original poster
Mar 22, 2008
141
0
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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

macrumors 6502a
Nov 10, 2006
726
0
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

macrumors 6502a
Jun 20, 2007
630
0
"The Google"
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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

Suspended
Nov 25, 2005
17,980
5,566
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

macrumors regular
Original poster
Mar 22, 2008
141
0
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)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.