自动引用计数 (ARC) 对内存泄漏的影响
我是 iOS 5 和 ARC 的新手,请原谅我的愚蠢问题。
如果我们在项目中使用ARC,是不是就意味着根本不会出现内存泄漏的情况呢?
如果我们使用ARC,是否需要使用Instruments来检测内存泄漏和NSZombies?
I am new to iOS 5 and ARC, so pardon my silly question.
If we use ARC in our project, does it mean that there wont be any memory leaks at all.
Is there a need to use Instruments for detecting memory leaks and NSZombies if we use ARC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ARC 将帮助您消除某些类型的泄漏,因为您不会忘记
release
或autorelease
单个对象。例如,这种类型的错误是不可能发生的:但是,ARC 不会消除由保留周期引起的泄漏。您仍然需要通过使用弱引用或在循环泄漏之前手动打破循环来消除保留循环。例如,当我们开始更多地使用块时,块/自保留循环变得更加常见。 过渡到 ARC 发行说明讨论如何使用弱引用来避免这些循环。
ARC will help you eliminate certain types of leaks, because you won't forget to
release
orautorelease
single objects. For example, this type of error becomes impossible:However, ARC will not eliminate leaks caused by retain cycles. It's still up to you to eliminate retain cycles, either by using weak references or by manually breaking the cycles before they become leaked. For example, as we start to use blocks more, block/self retain cycles become much more common. The Transitioning to ARC Release Notes discuss how to avoid these cycles using weak references.
不,这并不能防止内存泄漏的发生。在使用引用计数的运行时中会发生的情况是,有时您的代码会留下悬空引用,然后对象不会被释放。写出好的代码仍然取决于您。
No, that does not prevent memory leaks from happening. What happens in runtimes with reference counting, is that sometimes your code leaves dangling references, and then objects are not freed. It's still up to you to write good code.
可能仍然存在泄漏——在您的程序中以及您使用的库中。同样,ARC 仅适用于 ObjC 对象 - 您可以轻松泄漏任何不是 objc 对象的堆分配(例如
malloc
/new
)。是的。前面的回复应该详细说明为什么不能保证您的程序不存在这些问题。另外,如果你做了愚蠢的事情,编译器可能会出错,如果没有正确保护你的数据(例如并发执行),你肯定会导致问题。
There may still be leaks -- In your program, and in the libraries you use. As well, ARC only applies to ObjC objects - you can easily leak any heap allocation which is not an objc object (e.g.
malloc
/new
).Yes. The previous response should detail why your program is not guaranteed to be free of these problems. Also, the compiler can get it wrong if you do silly things, and you can certainly cause problems if don't protect your data properly (e.g. concurrent execution).