R – Is it right to say that methods whoose names contain new, alloc, create or copy do not autorelease the objects they create

cocoa-touchiphonememory-managementobjective cuikit

As far as I know, only the "convenience" methods return created objects with an -autorelease, so that the receiver is not responsible for the memory of that object. But because Apple says that when you use a method that has a name consisting of "new", "alloc", "create" or "copy", you're responsible for releasing the object that method returns, I think that these methods should not -autorelease these objects. Otherwise they would be "convenient" and the receiver would not be responsible at all as long as he does not -retain the received object. Did I get that right?

Best Answer

The rule of thumb is that you own (and therefore must release) any object that you

  • alloc (using a method starting "alloc" or "new"*)
  • copy (using any method containing the word copy)
  • retain explicitly yourself

*new combines an alloc and an init into one method.

Convenience methods return objects that are autoreleased. These are valid for the scope of the method you receive them in and can be passed back to a calling method (generally). If you want to keep them longer than that, you need to retain them.