Return autorelease objects and Apple's documentation


In the Using Autorelease section p. 67, the text says “NSString, like many other classes in the iOS SDK, includes convenience methods that return autoreleased objects. . .” The Apple documentation for stringWithFormat reads: “Returns a string created by using a given format string as a template into which the remaining argument values are substituted.”

My question is, how would one know that the string that is returned by stringWithFormat is an autoreleased string? Are all returns from convenience methods autoreleased? Or are certain convenience method returns autoreleased and others not? If the latter, how would I find out if the return is autoreleased or not as it seems the documentation does not include that info? Am I just missing something?


Convenience methods always return autorelease objects.

You should only release objects you created using alloc followed by init, copy (including mutable copy) or new.

NSString stringWith…: returns an auto release object while [[NSString alloc] init] returns an object which you should release as you are the one who created it.

Simple rule of thumb is : if you create an object, you should release it. Just don’t worry about objects being returned from other methods (They are auto released and already have a retainCount of 1 i.e they are marked for release automatically.)

I am also new to Objective C so I stand corrected here.


The convention is this:

Any method whose name is or begins with alloc, copy, or new returns a newly created object that has not been autoreleased, and must therefore be manually released.

Any other method returns an autoreleased (or otherwise neutral) object.

These rules are merely conventions, however. If you really wanted to, you could write a method called -newCar that returns an autoreleased instance of Car. Anyone who uses your method will be quite frustrated with you for doing so, however, because they will experience crashes when they additionally release the object.

All that said, if you have ARC turned on, you need to worry about none of this! :wink:


Thank you both for your replies. Not only have you answered my question, but your “rule of thumb” is very helpful as well.