Re: Why my object becomes invalid ?
Re: Why my object becomes invalid ?
- Subject: Re: Why my object becomes invalid ?
- From: Cameron Hayne <email@hidden>
- Date: Fri, 08 Aug 2003 22:03:47 -0400
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.
--
Cameron Hayne (email@hidden)
Hayne of Tintagel
_______________________________________________
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.