• 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: Another memory question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Another memory question


  • Subject: Re: Another memory question
  • From: "Alastair J.Houghton" <email@hidden>
  • Date: Tue, 26 Aug 2003 21:28:51 +0100

On Tuesday, August 26, 2003, at 08:49 pm, Jvrn Salewski wrote:

NSString *temp = [[NSString alloc] init];

You're allocating memory for an immutable NSString and initializes it with
nothing.

temp = [NSString stringWithString: @"~/Application User Data/dsf.plist"];

To the same memory region you assign an autoreleased NSString - the return
value of your convenience method. The previously allocated and initialized
String will not be accessible anymore --> a LEAK

Just to clarify this, writing something like

NSString *temp = [[NSString alloc] init];

temp = <some expression returning an NSString>;

is nearly always incorrect. What you need to remember is that "temp" is *a pointer* to the object, and *not* the object itself. So the initialiser makes temp point at a new, empty NSString, and the subsequent assignment is assigning not to the string but to the pointer, which is why there is a memory leak.

Lots of people get confused because they think that "temp" *is* the object when in fact it is a pointer to it.

Kind regards,

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

References: 
 >Re: Another memory question (From: Jörn Salewski <email@hidden>)

  • Prev by Date: Re: Formats supported by NSImage?
  • Next by Date: Re: NSString -> char* ? Use -[NSString UTF8String].
  • Previous by thread: Re: Another memory question
  • Next by thread: Re: Another memory question
  • Index(es):
    • Date
    • Thread