Skip navigation.
 
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
FROM : j o a r
DATE : Mon Feb 04 08:18:06 2008

Hello John,

On Feb 3, 2008, at 5:57 PM, John Engelhart wrote:

> This is bound to be an inflammatory subject.  That is not my intent, 
> and I mean no disrespect to the programmers who worked on the GC 
> system.  I'm quite sure that adding GC to Cocoa is a non-trivial, 
> near impossible task, filled with trade-offs between "really bad" 
> and "even worse."  Also understand that a bit of 'authoritative' 
> documentation or instruction can out right negate some of the points 
> I make below as I have had only the publicly available documentation 
> and my (relatively brief 2-3 months) experiences with the 10.5 GC 
> system to form these opinions....



Given that GC in Leopard is a 1.0 release I think it's to be expected 
that there will be bugs and room for improvement in both the 
implementation and the documentation. The best way to get the 
improvements that matters the most to you is to file targeted bug 
reports and enhancement requests.


> Anyone who's used garbage collection with C is probably familiar 
> with the Boehm Garbage Collector.  [...]
> From what I've pieced together, Leopards GC system is nothing like 
> this.



I think that the current "Architecture" section of the documentation 
gives a fairly good introduction and overview to what most developers 
need to know about the GC in Leopard. That said, I'm sure that you can 
think of things that you would like to see improved, and I encourage 
you to file enhancement requests wherever you do. The documentation 
department is very responsive and typically release multiple updates 
to the documentation per year.

As an example: The documentation currently basically only deals with 
Cocoa and CoreFoundation. It seems to me that you think that it's not 
clear enough on how to deal with for example a (char *), and I would 
agree. This would be a great enhancement request.


> This would explain the need for 'dual mode' frameworks, and that an 
> application that uses GC must be linked to frameworks that are all 
> GC capable.  This is because a non-GC framework would not actively 
> inform the GC system of its use of pointers, leading to random 
> crashes and what not as the GC system reclaimed memory that was 
> actively in use.



Manual memory management and automatic memory management is 
sufficiently different that you need to change your coding patterns to 
adapt to either mode. I don't think that you could point to any 
environment where you can run non-trivial code in either manual or 
automatic memory management without changes.

Finalizers have a different purpose in life than dealloc methods. You 
can't automatically turn dealloc methods into finalizers, and you 
can't just skip over them either. You need to have different code 
paths depending on the mode you choose for your code.


> And herein lies the reason why I believe Leopards GC system is 
> fundamentally and fatally flawed, and should in fact not be used at 
> all.  There are several possible 'solutions' to this, but you'd 
> better get it right or you're going to be stuck with race conditions 
> of the most insidious nature imaginable.  Adding fuel to the fire, 
> it's not clear what the 'right' solution is, or if there even is one.



Not to say that the engineers at Apple never make any mistakes, but do 
you really think that Apple would release something like this if what 
you say was true?  :-)


> One might argue that, per the __strong documentation, the ivar 
> requires the __strong type qualifier.  This is, at best, non-
> obvious, and considering that the documentation makes references to 
> 'objects' almost exclusively, one can also argue that this pointer 
> does not qualify.  But this points to a much bigger problem:  anyone 
> who has used UTF8String and not qualified it as __strong has a race 
> condition just waiting to happen.  This is also not a problem that 
> can be fixed with a patch to Foundation in the next Mac OS X 
> version- every program that has not qualified their use of 
> UTF8String with __strong must be recompiled and re-released as there 
> is nothing a shared library fix can do about this.



As a generalization I think it's fair to say that Apple will only fix 
bugs in *their* code by updates to Mac OS X, any bugs in *your* code 
must be fixed by you. If you have made a GC bug in one of your 
shipping applications - because you lacked sufficient documentation to 
get it right, or for any other reason - it's quite likely that you 
will have to issue an update to fix that bug.


> [...] A consequence of all of this is that you must not pass 
> pointers that may have been allocated by the garbage collector to 
> any C function in a library.  For example,
>
> printf("String: %s\n", [@"Hello, world!" UTF8String]);
>
> passes a GC allocated pointer to a C library function, which almost 
> assuredly does not have the proper write barrier logic in place to 
> properly guard the pointer.  This example is innocent enough, and 
> likely to work due to its short lived nature, but it's easy to think 
> of examples where the pointer passed to a C function, say an SQLite3 
> call, can cause no end of problems if that pointer happens to be 
> reclaimed in the middle of the function call.



I think that you forget, and this might be at the heart of your 
worries, that any pointer found on the *stack* is treated as a root. 
Being a root it will not be collected, and neither will anything that 
it in turn references.


Cheers,

j o a r

Related mailsAuthorDate
mlUse of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 4, 02:57
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Quincey Morris Feb 4, 05:09
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful j o a r Feb 4, 08:18
mlre: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Ben Trumbull Feb 4, 10:19
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 4, 14:11
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Greg Titus Feb 4, 16:39
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Sean McBride Feb 4, 22:40
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 5, 01:14
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Chris Hanson Feb 5, 02:21
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Jonathon Mah Feb 5, 08:47
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 5, 13:40
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 6, 10:39
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 6, 12:15
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 6, 15:59
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Tsai Feb 6, 16:12
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful glenn andreas Feb 6, 16:45
mlBug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful) Alastair Houghton Feb 6, 16:55
mlRe: Bug in CF/NSString's no-copy constructors (was Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful) Michael Tsai Feb 6, 17:46
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Clark Cox Feb 6, 17:47
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Jean-Daniel Dupas Feb 6, 18:05
mlRe: Bug in CF/NSString's no-copy constructors Alastair Houghton Feb 6, 18:46
ml[Moderator] Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Scott Anguish Feb 6, 18:46
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 6, 18:52
mlRe: Bug in CF/NSString's no-copy constructors Michael Tsai Feb 6, 19:29
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 6, 19:36
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Alastair Houghton Feb 6, 20:12
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful glenn andreas Feb 6, 21:23
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Sean McBride Feb 6, 21:47
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Ash Feb 6, 22:38
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 6, 23:49
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 02:06
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 7, 02:40
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Chris Suter Feb 7, 02:48
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 02:48
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Clark Cox Feb 7, 03:52
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful glenn andreas Feb 7, 04:01
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Timothy Reaves Feb 7, 04:08
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Brady Duga Feb 7, 04:22
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 07:14
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 7, 07:14
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Ash Feb 7, 23:58
mlRe: Bug in CF/NSString's no-copy constructors John Engelhart Feb 8, 03:31
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful John Engelhart Feb 8, 05:23
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Greg Parker Feb 8, 05:30
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Michael Tsai Feb 8, 05:56
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Hamish Allan Feb 8, 18:35
ml[Moderator] Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful Scott Anguish Feb 8, 22:17