The garbage man cometh
Garbage collection in Objective-C is confirmed for Leopard.
There's no confirmation yet, but some encouraging rumors say that Leopard will include fully-supported garbage collection in the Objective-C runtime.
I replied to his blog, posting a link to part two of my “Avoiding Copland 2010” series from September of last year. My article included a link to part of Apple's developer documentation that mentioned garbage collection in Objective-C in the context of the -finalize method in the NSObject class. When I revisited the link, however, the reference to garbage collection was gone.
The reference still exists in the developer documentation on my local system. I'm not sure if my dev tools are entirely up to date, so maybe it was removed in an update that I neglected to download and install. If you have the developer tools installed on your Mac, you can see for yourself by looking in the NSObject documentation in the ADC Reference Library on your hard drive. It reads, in part:
Called by the garbage collector when the receiver is not referenced by other objects.
The garbage collector invokes this method on the receiver before disposing of the memory it uses. When garbage collection is enabled, this method is invoked instead of dealloc.
Anyway, that was over six months ago. Today, things are a lot more clear. As Jesper pointed out in his comment to Scott Stevenson's blog, the gcc manual page in Mac OS X today spells out the situation quite plainly. See for yourself; just type “man gcc” at your Terminal prompt and then search for the word “garbage.” You'll find this:
Enable garbage collection (GC) for Objective-C objects. The resulting binary can only be used on Mac OS X 10.5 (Leopard) and later systems, due to additional functionality needed in the (NeXT) Objective-C runtime.
I wonder how long that's been lurking there. Anyway, it was “all but confirmed” last year, and now it's “actually confirmed.” Of course, Apple can still backpedal and silently remove or modify that bit of documentation, just as the documentation for the -finalize method in the NSObject class was silently removed from Apple's web site. Until there's an official announcement (presumably at WWDC), Apple still has an out. But regardless, it's absolutely clear that garbage-collected Objective-C is planned for Leopard.
In the past, I rejected garbage-collected Objective-C as a long-term solution to the memory-managed API problem in Mac OS X, writing:
[T]here has to be some sort of gate between the world of managed code and the low-level, “unsafe” stuff. Java chose to go with a “pure” approach and sealed the gate shut permanently. With C#, Microsoft took a more pragmatic approach and just put a latch on the gate. Developers can drop down to “unmanaged” code, but it takes a deliberate act to do so.
In Objective-C, on the other hand, there is no gate; it's all just one big, grassy field. Raw C code is just a keystroke away at all times. Again, this arrangement is a strength today. But in the future, I think the desire for “safety by default” will eventually win out, and Objective-C's intimate relationship with C will be perceived as a dangerous relic.
I still believe that today, but garbage collection in Objective-C is at least a step in the right direction. To make it work, Apple can't afford to waffle.
In order to make [garbage collection in Objective-C] worthwhile, Apple will have to really commit to it. They can't just put it out there, a la the Java/Cocoa bridge. If you build it, they probably won't come. Apple has not had much success with the “do as I say, not as I do” policy lately. […] For garbage-collected Objective-C to have any chance of a healthy life, Apple will have to lead by example.
Garbage collection can't just be for the “beginners,” with the “pros” sticking with manual retain/release/autorelease. In order to make any real progress, Apple has to position garbage-collected Objective-C as The Way to do things in Cocoa going forward. And the only way that's going to fly is if Apple breaks in the GC implementation by using garbage-collected Objective-C in an important application. That's the only way for anyone, Apple included, to be sure that the implementation is up to snuff.
So, will there be any garbage-collected Objective-C applications shipping in Leopard? If so, will they be things like the Calculator application (which was once written using the Java/Objective-C bridge, if I remember correctly) or something bigger like, say, Apple Mail or Safari? That's just one more thing to watch for at WWDC 2006. Only three more months to go…
This article originally appeared at Ars Technica. It is reproduced here with permission.