自动引用计数 (ARC) 对内存泄漏的影响

发布于 2024-12-28 07:48:51 字数 135 浏览 4 评论 0原文

我是 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 技术交流群。

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

发布评论

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

评论(3

时光暖心i 2025-01-04 07:48:51

ARC 将帮助您消除某些类型的泄漏,因为您不会忘记releaseautorelease 单个对象。例如,这种类型的错误是不可能发生的:

myLabel.text = [[NSString alloc] initWithFormat:@"%d", 17];
// oops, just leaked that NSString!

但是,ARC 不会消除由保留周期引起的泄漏。您仍然需要通过使用弱引用或在循环泄漏之前手动打破循环来消除保留循环。例如,当我们开始更多地使用块时,块/自保留循环变得更加常见。 过渡到 ARC 发行说明讨论如何使用弱引用来避免这些循环。

ARC will help you eliminate certain types of leaks, because you won't forget to release or autorelease single objects. For example, this type of error becomes impossible:

myLabel.text = [[NSString alloc] initWithFormat:@"%d", 17];
// oops, just leaked that NSString!

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.

愛上了 2025-01-04 07:48:51

不,这并不能防止内存泄漏的发生。在使用引用计数的运行时中会发生的情况是,有时您的代码会留下悬空引用,然后对象不会被释放。写出好的代码仍然取决于您。

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.

热风软妹 2025-01-04 07:48:51

如果我们的项目中使用ARC,是不是就意味着根本不会出现内存泄漏的情况。

可能仍然存在泄漏——在您的程序中以及您使用的库中。同样,ARC 仅适用于 ObjC 对象 - 您可以轻松泄漏任何不是 objc 对象的堆分配(例如 malloc/new)。

如果使用ARC,是否需要使用Instruments来检测内存泄漏和NSZombies?

是的。前面的回复应该详细说明为什么不能保证您的程序不存在这些问题。另外,如果你做了愚蠢的事情,编译器可能会出错,如果没有正确保护你的数据(例如并发执行),你肯定会导致问题。

If we use ARC in our project, does it mean that there wont be any memory leaks at all.

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).

Is there a need to use Instruments for detecting memory leaks and NSZombies if we use ARC?

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).

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