Skip navigation.
 
mlRe: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
FROM : Timothy Reaves
DATE : Thu Feb 07 04:08:58 2008

On Feb 6, 2008, at 8:06 PM, John Engelhart wrote:
> --snip--
> Actually, I've thought of another example which addresses the use of 
> (or lack of) __strong unambiguously and still demonstrates the 
> problem:
>
> #import <Foundation/Foundation.h>
>
> @interface GCTest : NSObject {
>  const char *title;
> };
>
> - (void)setTitle:(const char *)newTitle;
> - (const char *)title;
>
> @end
>
> @implementation GCTest
>
> - (void)setTitle:(const char *)newTitle
> {
>  printf("Setting title.  Old title: %p, new title %p = '%s'\n", 
> title, newTitle, newTitle);
>  title = newTitle;
> }
>
> - (const char *)title
> {
>  return title;
> }
>
> @end
>
> int main(int argc, char *argv[]) {
>  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
>  GCTest *gcConstTitle = NULL, *gcUTF8Title = NULL;
>  void *ptr;
>
>  gcConstTitle = [[GCTest alloc] init];
>  gcUTF8Title = [[GCTest alloc] init];
>
>  [gcConstTitle setTitle:"Hello, world!"];
>  [gcUTF8Title setTitle:[[NSString stringWithUTF8String:"Hello, world
> \xC2\xA1"] UTF8String]];
>
>  NSLog(@"Test: %@", @"hello");
>  [[NSGarbageCollector defaultCollector] collectExhaustively];
>  NSLog(@"GC test");
>
>  printf("gcConstTitle title: %p = '%s'\n", [gcConstTitle title], 
> [gcConstTitle title]);
>  printf("gcUTF8Title  title: %p = '%s'\n", [gcUTF8Title title], 
> [gcUTF8Title title]);
>
>  [gcConstTitle setTitle:NULL];  // Must clear the pointer before 
> popping pool.
>  [gcUTF8Title setTitle:NULL];
>
>  [pool release];
>  return(0);
> }
> [<email_removed>] /tmp% gcc -framework Foundation -fobjc-gc-only -
> o gc -g gc.m
> [<email_removed>] /tmp% ./gc
> Setting title.  Old title: 0x0, new title 0x1ea4 = 'Hello, world!'
> Setting title.  Old title: 0x0, new title 0x1011860 = 'Hello, worldĄ'
> 2008-02-06 18:32:35.712 gc[18108:807] Test: hello
> 2008-02-06 18:32:35.798 gc[18108:807] GC test
> gcConstTitle title: 0x1ea4 = 'Hello, world!'
> gcUTF8Title  title: 0x1011860 = 'Hello, world'
> Setting title.  Old title: 0x1ea4, new title 0x0 = '(null)'
> Setting title.  Old title: 0x1011860, new title 0x0 = '(null)'
>
> Oddly, I had to add a second NSLog() in order to get some kind of 
> lossage, but I think it's fair to chalk this up to the semi-random 
> nature of allocations.
>
> The above example is now perfectly legal by everyones definition of 
> how things were under retain/release, and I correctly clear the 
> pointer before it goes out of scope, and demonstrates that the GC 
> system can, and does, reclaim live data out from under you.
>

--snip--

"
UTF8String
Returns a null-terminated UTF8 representation of the receiver.

- (const char *)UTF8String

"

Direct from Apple's docs.  You seriously need to go back to basics if 
you don't understand how screwed up your logic here is!

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