• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful


  • Subject: Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
  • From: Timothy Reaves <email@hidden>
  • Date: Wed, 6 Feb 2008 22:08:58 -0500


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);
}
[johne@LAPTOP_10_5] /tmp% gcc -framework Foundation -fobjc-gc-only - o gc -g gc.m
[johne@LAPTOP_10_5] /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!


_______________________________________________

Cocoa-dev mailing list (email@hidden)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


References: 
 >Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: John Engelhart <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: Alastair Houghton <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: John Engelhart <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: Chris Hanson <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: John Engelhart <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: "Hamish Allan" <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: Alastair Houghton <email@hidden>)
 >Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful (From: John Engelhart <email@hidden>)

  • Prev by Date: Re: Searching for "whole word" in NSString
  • Next by Date: Re: Memory(?) issue
  • Previous by thread: Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
  • Next by thread: Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
  • Index(es):
    • Date
    • Thread