views:

744

answers:

3

If I have a parameter passed to a method, do I need to release the parameter at the end of the method?

+36  A: 

No. Think NARC: "New Alloc Retain Copy". If you are not doing any of those things, you don't need to release.

Andiih
+1 for the acronym
Dave DeLong
Seriously. That is awesome. Works well if you assume a Stooge's voice and think "Narc narc narc" as you analyze your code for memory management issues...
bbum
Thats a lot of rep for a question with 0 up votes there.
thyrgle
thyrgle - no one was more surprised than me! Mainly because I thought nearly everyone new the NARC acronym. Seems like it was (good) news to many!
Andiih
+2  A: 

You need to release them only, if you retain them in your method. The convention is, that the caller is responsible to make sure, that the objects passed as arguments live at least as long as the call is active.

Dirk
+6  A: 

Please read the Cocoa memory management guidelines. The following rule is relevant to your question:

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.

Clearly you did not obtain the parameters by creating them (in your method). So the only part that you need to worry about is whether you retained them in the method. If you did, you must release or autorelease them. If you did not, you must not release or autorelease them.

JeremyP