Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do you need to release parameters of methods at the end of them in Objective-C?

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

like image 802
marty Avatar asked May 19 '10 11:05

marty


2 Answers

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

like image 59
Andiih Avatar answered Sep 28 '22 13:09

Andiih


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.

like image 41
JeremyP Avatar answered Sep 28 '22 12:09

JeremyP