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






Cocoa mail archive

