Re: How can I release an NSString?
Re: How can I release an NSString?
- Subject: Re: How can I release an NSString?
- From: Arthur VIGAN <email@hidden>
- Date: Sun, 19 Jan 2003 11:12:24 +0100
That's a very clear answer, thank you very much.
Arthur
Le dimanche, 19 jan 2003, ` 10:45 Europe/Paris, Alan leigh a icrit :
G'day Arthur,
I may have this wrong, but as I understand it, stringWithString is a
convenience constructor, which means by default the object it makes is
auto-released. (The only time you need to specifically auto-release it
is if you use a [[NSWhatever alloc] init]) This means when you call
giveMeAString you need to retain what it passes you and release when
you are done with it. This way everything balances.... the retain
count of an object returned from giveMeAString is 0. So you won't leak
memory. You might run into trouble if you do:
NSString *someString = [self giveMeAString];
because someString might point to something which is autoreleased. So
what you should do is;
NSString *someString = [[self giveMeAString] retain];
the object returned retain count goes to 1 and it's up to the caller
to release it. Try looking here:
http://www.stepwise.com/Articles/Technical/HoldMe.html
or here:
http://www.stepwise.com/Articles/Technical/2001-03-11.01.html
Does this help?
Cheers,
Alan
On Sunday, January 19, 2003, at 08:11 PM, Arthur VIGAN wrote:
Hi,
I have a quite simple question, which I find annoying...
Let's consider the following method:
- (NSString *)giveMeAString
{
NSString *aString;
aString = [NSString stringWithString:@"Hello World!"];
return aString
}
My question is: is memory leaking in this code, because the string is
never released? Should I use autorelease?
Thanks in advance for your answers to this basic question,
Arthur
_______________________________________________
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.
_______________________________________________
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.