Re: How can I release an NSString?
Re: How can I release an NSString?
- Subject: Re: How can I release an NSString?
- From: Brock Brandenberg <email@hidden>
- Date: Sun, 19 Jan 2003 12:09:23 -0600
Arthur,
There's a misunderstanding in Alan's answer.
>
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:
The retain count of an object returned from giveMeAString is not 0, it's 1.
But since the object is autoreleased, it will get decremented to 0
automatically for you when the current autorelease pool is released itself,
then the object would go away.
If you were to subsequently retain the object like Alan shows in his
example, the retain count would go up to 2 temporarily, but when the current
autorelease pool is released, the object would have release called
automatically and the retain count would drop to 1. At this point, you would
be the only one explicitly retaining the object, so it's your job to release
it whenever you're done with it.
Typically, the autorelease pool created by the application is the only one
that many programmers use, and it itself gets released and recreated at the
end of every run loop. So any autoreleased object added to the pool will get
"release" called for it at the end of every run loop the number of times
that the object was "autoreleased".
You really need to read up on the retain count concept and on autorelease
pools. Until memory allocation is clear in your head, it will continue to
haunt you in your Cocoa programming endeavors.
Brock Brandenberg
----- industrial design @ www.bergdesign.com ------
_______________________________________________
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.