views:

736

answers:

4

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.

+17  A: 

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.

Joey Hagedorn
To clarify one point: In the case of a singleton, *the class* owns its one and only instance, which is why it doesn't release or autorelease it. In all other cases (generally convenience factory methods, such as `stringWithUTF8String:`), the class does not intend to own the instance, so it does autorelease it.
Peter Hosey
To reinforce what Joey has said - the only thing you care about is object ownership as stated in the Cocoa memory management rules. The question is "do I own the returned object" not "did the method I called autorelease it".
Jim Correia
+1 for a good answer. See my answer for a little additional analysis.
Quinn Taylor
A: 

The general rule is if the name of the method contains alloc, new, or copy, then you own it. Otherwise it may be autoreleased or it is taken care of by someone else, i.e. you don't own it so you don't have to worry about it.

mahboudz
+1  A: 

Convenience methods usually return autoreleased objects. E.g. [NSMutableArray array] is equivalent to [[[NSMutableArray alloc] init] autorelease].

The fact that these are class methods, should not lead you to the conclusion that all class methods return autoreleased objects. It's all about the naming convention. If the menthod has a alloc*, copy, new* name, then you will be the owner of the returned object. I.e. it will have a retain count of at least 1 when passed to you.

Pierre Bernard
A: 

It's worth remembering that class methods don't have to return an object at all — any method can return anything (including structs, primitive types, etc.) or nothing (void). There is nothing in the Objective-C language that requires class methods to return an unowned object (meaning the caller is not required to release/autorelease it).

That said, @Joey's answer is a great one. The rule of thumb for class methods that return an object is that it should be autoreleased. Breaks from convention should be clearly documented to avoid causing memory leaks in client code.

Quinn Taylor