views:

126

answers:

3

I know my way around Objective-C and I have experience with garbage collection from .NET, although I never used it in objective-c. I write my code without using it.

Now I'm thinkig about using one of the frameworks (Blocks) which is available as GC-only. My question is - can I still use the framework without any changes to my current non-GC code and without using GC myself?

+3  A: 

As stated in the garbage collection programming guide, "Code compiled as GC Required is presumed to not use traditional Cocoa retain/release methods and may not be loaded into an application that is not running with garbage collection enabled."

So no, unfortunately. But how much work it would be to adopt garbage collection depends on your app. You might try testing to see if it looks like a big project. (It often is, but sometimes it's not so bad.)

Chuck
A: 
alesplin
Actually, compiling GC-supported means it can run in either GC mode or managed-memory mode — but only one or the other in any given program. It's basically a way for libraries to support both kinds of apps. You still need to run under GC in order to use GC-only code, in which case the GC-supported code's retains and releases will be no-ops.
Chuck
edited to further clarify. I'm apparently having issues getting my complete thoughts together today.
alesplin
This answer is incorrect.
bbum
so to makes things clear. if my code doesn't use GC, other frameworks doesn't use GC but Blocks uses GC than setting "GC Required" will work since every retain/release will be ignored and GC will be used instead?so there would be a problem only in situations if my code would use GC and at least one framework wouldn't support it (or of course if i would include GC-only framework and didn't set the project to GC-only - than code from that framework would leak).am I right?
shw
If your project is set to GC-required then any frameworks you use MUST be GC-supported or GC-required. You can't use frameworks not written with GC in mind. Switching your project to use garbage collection is straightforward but there are things that you must understand before you do so. You should read the guide thoroughly and make sure you understand it: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/GarbageCollection/index.html
Rob Keniger
If the code is not compiled GC supported or GC required, it cannot and will not be loaded into a GC'd application. No "it'll leak" about it. It just won't work at all.
bbum
+3  A: 

A process is either GC or non-GC. That is, all Objective-C will either always be GC'd or will never be GC'd. There is no mixing of the two memory models in a single process. You cannot mix a GC only framework with a non-GC only framework.

When building a framework, you can set GC to "supported" at which point in time the framework could be used in either a GC'd or a non-GC'd process. However, you will have to maintain correctness for both running environments separately.

What is this "Blocks" framework to which you refer? If you are talking about Blocks, the language feature shipped in Snow Leopard's Objective-C, then it works just fine under both GC and non-GC.

bbum
I believe it's this one: http://github.com/jessegrosjean/blocks
Rob Keniger