Objective-C 中是否需要在方法末尾释放参数?

发布于 2024-09-01 23:37:37 字数 40 浏览 2 评论 0原文

如果我有一个参数传递给一个方法,我是否需要在方法结束时释放该参数?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

预谋 2024-09-08 23:37:37

不。想想NARC:“新分配保留副本”。如果您没有做任何这些事情,则不需要释放。

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

原谅过去的我 2024-09-08 23:37:37

请阅读 Cocoa 内存管理指南。以下规则与您的问题相关:

如果您使用名称以“alloc”或“new”开头或包含“copy”的方法(例如,alloc、newObject 或 mutableCopy)创建该对象,或者向该对象发送一个保留消息。您有责任使用release或autorelease放弃您拥有的对象的所有权。任何其他时候您收到对象时,都不得释放它。

显然,您没有通过创建参数(在您的方法中)来获取参数。所以你唯一需要担心的是你是否在方法中保留了它们。如果这样做了,您必须释放或自动释放它们。如果没有,则不得释放或自动释放它们。

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.

油焖大侠 2024-09-08 23:37:37

仅当您在方法中保留它们时,您才需要释放它们。约定是,调用者有责任确保作为参数传递的对象至少在调用处于活动状态时存在。

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.

墨小墨 2024-09-08 23:37:37

除非您直接使用基础对象,否则您现在应该将所有这些委托给 ARC。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文