• 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: Why my object becomes invalid ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Follow-Ups:
    • Re: Why my object becomes invalid ?
      • From: Chris Hanson <email@hidden>
    • Re: Why my object becomes invalid ?
      • From: Shawn Erickson <email@hidden>
References: 
 >Why my object becomes invalid ? (From: "kubernan @ 10191 Tec." <email@hidden>)

  • Prev by Date: Re: Loading 2 classes with same name
  • Next by Date: Re: Why my object becomes invalid ?
  • Previous by thread: Why my object becomes invalid ?
  • Next by thread: Re: Why my object becomes invalid ?
  • Index(es):
    • Date
    • Thread