Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Does one have to release objects allocated by class functions in NSString

Classess like NSString and NSData have class methods which return a new object of the given class.

Once the function returns, does one have to release the object after use? Or is it added to the autorelease pool by the class methods?

Thanks,

-TRS

MacBook, Mac OS X (10.5.2)

Posted on May 25, 2008 1:17 PM

Reply
Question marked as Best reply

Posted on May 25, 2008 2:09 PM

No. In general, unless the class method is name is something like 'alloc' then you aren't responsible for releasing the object. I can't remember the exact rules off hand, and memory management is still something I have to fully get up to speed on.

I suggest you read the memory management document in the 'performance' section of the developer documents though - it explains things pretty well.
5 replies
Question marked as Best reply

May 25, 2008 2:09 PM in response to trshivku

No. In general, unless the class method is name is something like 'alloc' then you aren't responsible for releasing the object. I can't remember the exact rules off hand, and memory management is still something I have to fully get up to speed on.

I suggest you read the memory management document in the 'performance' section of the developer documents though - it explains things pretty well.

May 26, 2008 12:28 AM in response to mdecaro

Thanks to both of you for your responses. It was quite helpful.

Talking about autorelease. What is the scope of the objects that are returned by the class methods (except for init) ? Is it available until the pool created in main() is released? That would be too late i think. Ideally i would think it would be until the lifetime of the method that the class method is called in.

I would appreciate your insights into this.

Thanks,

-TRS

May 26, 2008 11:29 AM in response to trshivku

I believe that Foundation creates and destroys an autorelease pool on each pass through the runloop. Because of the runloop design, your code runs in response to an event (timer, input, etc). Once you return from your event handler, the stack eventually unwinds back up to the runloop, at which point the autorelease pool is drained.

In practice, just be conservative and assume the object will stay alive until the end of your current function/method (unless you create additional autorelease pools yourself).

May 27, 2008 12:50 PM in response to trshivku

You only release objects that you own.

Objects that you own are defined as those that you created with

alloc
copy
retain

Objects that are created with the "convenience" methods of a class, namely methods that have terms like "initWith...." or "stringWith.....", etc. are owned by the class object. They are autoreleased, which means they are released after the current run through the run loop.

So if you want to keep a reference to them, either retain them or copy them.


NSString *myString = [[NSString alloc] init]; // Belongs to you
NSString *myString = [NSString stringWithFormat:@"%@", anObject]; //Belongs to NSString

Does one have to release objects allocated by class functions in NSString

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.