views:

257

answers:

2

Do either or both of these date pointers requires a [release] when I'm done with them. How would i know this? I'm unsure since I'm not doing init explicitly.

NSDate *date = [NSDate date];

NSDate *date = [dateWithTimeIntervalSince1970:100000000];
+2  A: 

No, both of the returned dates from those methods are autoreleased. You don't need to worry about their memory management, though to be a good citizen, setting the pointer to nil when you're done with them would be a good idea.

As a general rule, you can follow what I call the "CARN" rule. in Cocoa/Cocoa Touch any method that has the words Copy, Alloc, Retain, or New in them will need to be released by you at some point. These are naming conventions applied to methods that return objects with a retain count of +1. The class that calls these methods "owns" the object and is responsible for releasing it when it's finished with it.

Hope this helps.

Jasarien
+1  A: 

Both are autoreleased, that is you don't need to release them yourself. The rule of thumb is that you own an object if you send +alloc or -copy or explicitly retain it:

  • [[SomeClass alloc] init...]
  • [someObject copy]
  • [some object retain]

If you own an object you must release it. +new is a shortcut to +alloc and -init.

Costique
Ok, but is it incorrect to release it? When will it get released if I do not do so explicitly?
glutz78
Yes, it is wrong to release an object you don't own. This is called "over-releasing" and will cause a crash when -release (or any other message, for that matter) is sent to an already-released object. Autoreleased objects are actually released when the enclosing NSAutoreleasePool is released/drained (if you don't use them explicitly, this happens at the end of each cycle of the event loop). See "Memory Management Programming Guide for Cocoa".
Costique