R – Do all class methods return an autoreleased object

cocoaiphonememory-managementobjective-c

I'm re-reading the first few chapters of Cocoa Programming for Mac OS X and the author states that one of NSCalendarDate's class method returns an autoreleased object. I always assumed that all class methods returned an autoreleased object (since there's no alloc involved).

Are there any class methods which you have to specifically retain?

Thanks.

Best Solution

Class methods, just like instance methods, should adhere to the standard Cocoa memory management rules.

You take ownership of an object if you create it using a method whose name begins with “alloc” or “new” or contains “copy” (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

Presumably they are returning an autoreleased object, or a reference to a singleton or something like that. Either way, you need not release the object unless it started with "alloc" or "new" or contained "copy". You need not retain it unless you're looking to keep it around past the scope of the current autorelease pool, by storing it in an iVar or something like that.