Skip navigation.
 
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
FROM : Quincey Morris
DATE : Mon Feb 04 05:09:14 2008

On Feb 3, 2008, at 17:57, John Engelhart wrote:

> In order for leopards GC system to function properly, the compiler 
> must be aware of all pointers that have been allocated by the GC 
> system so that it can wrap all uses of the pointer with the 
> appropriate GC notification functions (objc_assign*).  Note that 
> this is subtly different that the definitions and examples used in 
> 'Garbage Collection Programming Guide'.  From 'Garbage Collection 
> Programming Guide', 'Language Support':
>
> __strong
> Specifies a reference that is visible to (followed by) the garbage 
> collector (see “How the Garbage Collector Works”).
>
> __strong modifies an instance variable or struct field declaration 
> to inform the compiler to unconditionally issue a write-barrier to 
> write to memory. __strong is implicitly part of any declaration of 
> an Objective-C object reference type. You must use it explicitly if 
> you need to use Core Foundation types, void *, or other non-object 
> references (__strong modifies pointer assignments, not scalar 
> assignments).
>
> ----
>
> This is a deceptive description.  /ANY/ pointer that holds a pointer 
> to memory that MAY be allocated from the garbage collector must be 
> marked __strong.  The compiler attempts to 'automagically' add 
> __strong to certain types of pointer references, specifically 'id' 
> and derivatives of 'id', namely class pointers (NSString *).


Interesting post. A couple of comments (that may just show I didn't 
absorb all of your argument):

-- The extent of the deception seems to be that __strong is an 
attribute of the declaration, not of the pointer, but the 
documentation confuses the two: the compiler must be aware of all 
*variables used for* pointers that have been allocated by the GC 
system, and *a single variable cannot be used at different times for 
pointers to memory in different allocation systems*. If there was a 
fix to the documentation, would you still say GC is broken?

-- It doesn't exactly surprise me that your sample code failed, 
because my reading of the documentation (the section you quoted) is 
that it told you the rules and you didn't follow them -- by not 
putting __strong on the char* ivar in the version that failed. The 
only issue is whether -[NSString UTF8String] returns memory allocated 
from a GC-controlled pool or not. The documentation for the method says:

> Discussion
>
> The returned C string is automatically freed just as a returned 
> object would be released; you should copy the C string if it needs 
> to store it outside of the autorelease context in which the C string 
> is created.


This sounds like it hasn't been updated for Leopard, but I'd sure read 
it as telling me the return string comes from the same place objects 
come from -- GC memory. And therefore any stored pointer to it would 
need a __strong or a __weak on its variable. Or, as stipulated, the 
result could be copied into malloc memory before being used. (The 
picture in the GC documentation suggests that malloc memory isn't GC-
controlled, although I didn't find any text to state this absolutely. 
Maybe it's too obvious to say.)

Or did I miss your point?

-- I too puzzled over the meaning of the stuff in the GC document 
about write barriers, which I agree raises more questions than it 
answers. In the end, I came to the conclusion that "write barriers" in 
this case were nothing to do with protecting the integrity or lifetime 
of any pointer, but rather a pragmatic hint to *this* GC 
implementation about how hard it might work at collection on any given 
occasion.

If the documentation were changed to use the phrase "collection 
performance hints" instead of "write barriers", would you still say GC 
is broken?

-- So I wonder if the problem is that GC is broken, or just annoyingly 
fussy and poorly documented as regards to non-object memory.

I hope you'll post more analysis of the problem. I (with a sigh of 
relief) jumped ship from the SS Retainer, so it matters to me if I'm 
now sailing towards that world of hurt you foreshadow. :)

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