Re: NSMutableString question
Re: NSMutableString question
- Subject: Re: NSMutableString question
- From: Jason Coco <email@hidden>
- Date: Tue, 16 Sep 2008 21:10:34 -0400
On Sep 16, 2008, at 20:59 , Dave DeLong wrote:
The general rule with convenience class methods like that is that
they return an autoreleased object. What that means is that unless
you retain it, it will disappear at some time in the future
(whenever the current AutoreleasePool gets drained).
Yes, as Dave says, convenience methods like [NSMutableString string]
get autoreleased, so you don't have to do anything at all to reclaim
the memory, but you do have to make sure you retain the string if you
want it to persist anywhere outside it's current function/method
scope. (BTW, when I say this, it doesn't mean that the memory /will/
get freed when the function returns, just that you have no guarantee
that the object is valid once the function returns--unless you retain
it)
So if you want to "reclaim" the space, you don't have to do
anything. If you want to reclaim it immediately, I think you ought
to be able to do:
NSString * str = [NSMutableString string];
//do stuff with str
[[str retain] release];
HOWEVER, that might cause funky things to happen with the
autorelease pool. So the best idea is to do nothing and let the
autorelease pool take care of it.
Don't do that... if you're working with a limited-memory environment,
or you are creating a lot of objects, your best bet is to actively
manage your memory. Create your mutable string with alloc/init and
then release it when you're done:
NSMutableString *str = [[NSMutableString alloc]
initWithCapacity:someAssumedCapacity];
/* do stuff */
[str release];
Or, if you're creating lots of short-lived things in a loop or
something, create your own autorelease pool:
while (loopConditionIsTrue) {
NSAutoreleasePool *myPool = [[NSAutoReleasePool alloc] init];
NSMutableString *str = [NSMutableString string];
/* do stuff */
[myPool drain]; // all your autorelease objects created within the
loop will be freed here
}
/jason
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden