开发者

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

开发者 https://www.devze.com 2022-12-30 16:03 出处:网络
If I have a parameter passed to a method, do I need to release the parameter at the end of the method?开发者_开发技巧No. Think NARC: \"New Alloc Retain Copy\". If you are not doing any of those things

If I have a parameter passed to a method, do I need to release the parameter at the end of the method?开发者_开发技巧


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


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.


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.


Unless you're working directly with foundation objects, you should be delegating all this to ARC by now.

0

精彩评论

暂无评论...
验证码 换一张
取 消