Objective-C 中是否需要在方法末尾释放参数?
如果我有一个参数传递给一个方法,我是否需要在方法结束时释放该参数?
If I have a parameter passed to a method, do I need to release the parameter at the end of the method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不。想想NARC:“新分配保留副本”。如果您没有做任何这些事情,则不需要释放。
No. Think NARC: "New Alloc Retain Copy". If you are not doing any of those things, you don't need to release.
请阅读 Cocoa 内存管理指南。以下规则与您的问题相关:
显然,您没有通过创建参数(在您的方法中)来获取参数。所以你唯一需要担心的是你是否在方法中保留了它们。如果这样做了,您必须释放或自动释放它们。如果没有,则不得释放或自动释放它们。
Please read the Cocoa memory management guidelines. The following rule is relevant to your question:
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.除非您直接使用基础对象,否则您现在应该将所有这些委托给 ARC。
Unless you're working directly with foundation objects, you should be delegating all this to ARC by now.