• 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: "Clark Cox" <email@hidden>
  • Date: Wed, 6 Feb 2008 18:52:03 -0800

On Feb 6, 2008 5:06 PM, John Engelhart <email@hidden> wrote:
>
> Actually, I've thought of another example which addresses the use of
> (or lack of) __strong unambiguously and still demonstrates the problem:

No, you're just rehashing and reformulating the same argument over and
over again. You're not listening.


>
> #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;

This is a bad thing to do in pre-GC Cocoa, it is still a bad thing to
do in post-GC Cocoa. Don't do it. If you want to store the result of
calling UTF8String, copy it. Period.

> }
>
> - (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]];

At this point, the temporary NSString object has exactly zero roots.
Why are surprised that it goes away?

>    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);
> }


> 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.

No it is not. Holding on to the result of UTF8String for longer than
the lifetime of the NSString from which it came is not legal. How hard
is that to understand?


--
Clark S. Cox III
email@hidden
_______________________________________________

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: default expand outline view items
  • Next by Date: Re: Use of Mac OS X 10.5 / Leopards Garbage Collection Considered Harmful
  • 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