Re: Why my object becomes invalid ?
Re: Why my object becomes invalid ?
- Subject: Re: Why my object becomes invalid ?
- From: Shawn Erickson <email@hidden>
- Date: Fri, 8 Aug 2003 20:03:21 -0700
On Friday, August 8, 2003, at 07:03 PM, Cameron Hayne wrote:
On 8/8/03 9:09 PM, "kubernan @ 10191 Tec." <email@hidden> wrote:
myString = [[NSString alloc] init];
myString = [myString stringByAppendingString:@"test"];
The variable 'myString' is a data member and hence must be retained.
I guess you recalled reading that if you alloc'ed an object then you
didn't
need to retain it. But in the above code, you overwrite the pointer
that got
back from the alloc & init with the result that you get from the
stringByAppendingString in the second line. Thus your original, nicely
allocaed string object is no longer in the picture. After the second
line,
mystring is pointing at the object that was alloacted by
stringByAppendingString and that object will be autoreleased. So it
goes
away after this method returns.
In addition, you have a memory leak since you have lost track of the
original string object from your alloc & init and so you can't ever
release
it.
Also it would be easier to just do the following:
myString = @"test";
The above results in an compile time allocated NSString object
(constant). If that is not what you want consider doing the following:
myString = [[NSString stringWithString:@"test"] retain];
This will get you a string that can be released when no longer needed,
however since in this case it comes from a @"....." definition for a
string the backing string will continue to exist because it is a
constant string.
For fun...
NSString* string = @"first string";
NSLog(@"%@ %d %@", string, [string retainCount], [string class]);
[string release];
NSLog(@"%@ %d %@", string, [string retainCount], [string class]);
//retain count is -1 because the object is compile time allocated
NSString* string2 = [[NSString stringWithString:string] retain];
NSLog(@"%@ %d %@", string2, [string2 retainCount], [string2 class]);
[string2 release];
NSLog(@"%@ %d %@", string2, [string2 retainCount], [string2 class]);
//retain count still one because of autorelase in stringWtihString
//after the current autorelease pool is deallocated the string will
go away
NSString* string3 = [[string stringByAppendingString:@", second
string"] retain];
NSLog(@"%@ %d %@", string3, [string3 retainCount], [string3 class]);
[string3 release];
NSLog(@"%@ %d %@", string3, [string3 retainCount], [string3 class]);
//retain count still one because of autorelase in
stringByAppendingString,
//after the current autorelease pool is deallocated the string will
go away
2003-08-08 19:49:22.252 test[854] first string -1 NSConstantString
2003-08-08 19:49:22.252 test[854] first string -1 NSConstantString
2003-08-08 19:49:22.252 test[854] first string 2 NSCFString
2003-08-08 19:49:22.253 test[854] first string 1 NSCFString
2003-08-08 19:49:22.253 test[854] first string, second string 2
NSCFString
2003-08-08 19:49:22.253 test[854] first string, second string 1
NSCFString
-Shawn
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.