在Objective-C中使用ARC时,AutoRelease是否冗余? [英] Is AutoRelease redundant when using ARC in Objective-C?

查看:90
本文介绍了在Objective-C中使用ARC时,AutoRelease是否冗余?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您可能会认识到的那样,我对Objective-C还是陌生的,直到最近,我才真正了解所有这些AutoRelease恶意软件的必要性.我认为主要是因为我从ARC开始了Objective-C,并且没有接触过保留和释放的经验.

I'm pretty new to Objective-C, as you may gather, and until recently, I hadn't really understood the need for all this AutoRelease malarky. I think that's mostly because I've started Objective-C with ARC, and haven't had any exposure to doing retains and release.

无论如何,我现在的理解是,在ARC之前,如果您创建了一个对象并需要返回指向它的指针作为方法/函数的返回对象,则您将需要自动释放它,因为您无法执行此操作执行返回obj;"后的"[obj版本]"

Anyway, my understanding now is that pre-ARC, if you created an object and needed to return a pointer to it as the returning object of the method/function, you would need to autorelease it, because you are unable to do the "[obj release]" after doing "return obj;"

担心保留和发行对ARC来说不是问题.这是否意味着在我们自己的代码中,创建我们自己的自动发布对象真的有意义吗?即,正在执行[[[[Class alloc] init] autorelease]?根据我的收集,我们仍然应该设置自动释放池,但这仅仅是因为其他框架或库可能仍会返回自动释放的对象,但是我们不再需要自己明确创建自动释放的对象-这是一个合理的理解吗?

Worrying about retains and releases isn't an issue with ARC. Does this mean that in our own code, there is really point in creating our own autoreleased objects? Ie, doing [[[Class alloc] init] autorelease]? From what I've gathered, we should still setup autorelease pools, but only because other frameworks or libraries may still return autoreleased objects, but we no longer need to explicitly create autoreleased objects ourselves - is this a fair understanding?

谢谢, 尼克

推荐答案

使用ARC时,您不想自己进行任何内存管理.具体来说,您不会调用发布和自动发布,因为这一切都为您完成.实际上,如果您尝试自己管理内存,编译器可能会抱怨.

When using ARC, you do not want to do any memory management yourself. Specifically you will not be calling release and auto release because it does it all for you. In fact, the compiler should probably complain if you try to manage memory yourself.

您将只调用[[Class alloc] init];

我建议阅读此博客文章总体来说,具有一些非常好的ARC和内存管理背景.

I recommend reading this blog post for some really good background on ARC and memory management in general.

这篇关于在Objective-C中使用ARC时,AutoRelease是否冗余?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆