Re: Another memory question
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.