What kind of leaks does automatic reference counting in Objective-C not prevent or minimize?(Objective-C 中的自动引用计数不能防止或最小化什么样的泄漏?)
问题描述
在 Mac 和 iOS 平台中,内存泄漏通常是由未释放的指针引起的.传统上,检查您的分配、复制和保留以确保每个都有相应的发布消息一直是最重要的.
In the Mac and iOS platforms, memory leaks are often caused by unreleased pointers. Traditionally, it has always been of utmost importance to check your allocs, copies and retains to make sure each has a corresponding release message.
Xcode 4.2 附带的工具链通过最新版本的 LLVM 编译器引入了自动引用计数 (ARC),完全消除了这一点通过让编译器为你管理你的东西来解决问题.这很酷,它确实减少了许多不必要的、平凡的开发时间,并防止了许多粗心的内存泄漏,这些泄漏很容易通过适当的保留/释放平衡来修复.当您为 Mac 和 iOS 应用启用 ARC 时,甚至自动释放池也需要以不同方式管理(因为您不应再分配自己的 NSAutoreleasePool
).
The toolchain that comes with Xcode 4.2 introduces automatic reference counting (ARC) with the latest version of the LLVM compiler, that totally does away with this problem by getting the compiler to memory-manage your stuff for you. That's pretty cool, and it does cut lots of unnecessary, mundane development time and prevent a lot of careless memory leaks that are easy to fix with proper retain/release balance. Even autorelease pools need to be managed differently when you enable ARC for your Mac and iOS apps (as you shouldn't allocate your own NSAutoreleasePool
s anymore).
但是 其他 内存泄漏有哪些不可以防止我仍然需要提防?
But what other memory leaks does it not prevent that I still have to watch out for?
作为奖励,Mac OS X 和 iOS 上的 ARC 与 Mac OS X 上的垃圾收集有什么区别?
As a bonus, what are the differences between ARC on Mac OS X and iOS, and garbage collection on Mac OS X?
推荐答案
您仍然需要注意的与内存相关的主要问题是保留周期.当一个对象具有指向另一个对象的强指针,但目标对象具有指向原始对象的强指针时,就会发生这种情况.即使删除了对这些对象的所有其他引用,它们仍然会相互保持并且不会被释放.这也可能通过一系列对象而间接发生,这些对象链中的最后一个对象可能会引用较早的对象.
The primary memory-related problem you'll still need to be aware of is retain cycles. This occurs when one object has a strong pointer to another, but the target object has a strong pointer back to the original. Even when all other references to these objects are removed, they still will hold on to one another and will not be released. This can also happen indirectly, by a chain of objects that might have the last one in the chain referring back to an earlier object.
正是出于这个原因,存在 __unsafe_unretained
和 __weak
所有权限定符.前者不会保留它指向的任何对象,但会保留该对象消失并指向坏内存的可能性,而后者不保留该对象并在其目标被释放时自动将其自身设置为 nil.在这两者中,__weak
通常在支持它的平台上是首选.
It is for this reason that the __unsafe_unretained
and __weak
ownership qualifiers exist. The former will not retain any object it points to, but leaves open the possibility of that object going away and it pointing to bad memory, whereas the latter doesn't retain the object and automatically sets itself to nil when its target is deallocated. Of the two, __weak
is generally preferred on platforms that support it.
您可以将这些限定符用于委托等事物,您不希望对象保留其委托并可能导致循环.
You would use these qualifiers for things like delegates, where you don't want the object to retain its delegate and potentially lead to a cycle.
另外两个重要的内存相关问题是处理Core Foundation 对象和使用malloc()
为char*
等类型分配的内存.ARC 不管理这些类型,只管理 Objective-C 对象,因此您仍然需要自己处理它们.Core Foundation 类型可能特别棘手,因为有时它们需要桥接以匹配 Objective-C 对象,反之亦然.这意味着在 CF 类型和 Objective-C 之间进行桥接时,需要从 ARC 来回传输控制.添加了一些与此桥接相关的关键字,Mike Ash 在 他冗长的 ARC 文章.
Another couple of significant memory-related concerns are the handling of Core Foundation objects and memory allocated using malloc()
for types like char*
. ARC does not manage these types, only Objective-C objects, so you'll still need to deal with them yourself. Core Foundation types can be particularly tricky, because sometimes they need to be bridged across to matching Objective-C objects, and vice versa. This means that control needs to be transferred back and forth from ARC when bridging between CF types and Objective-C. Some keywords related to this bridging have been added, and Mike Ash has a great description of various bridging cases in his lengthy ARC writeup.
除此之外,还有其他一些不太常见但仍有潜在问题的案例,如 已发布的规范详细介绍.
In addition to this, there are several other less frequent, but still potentially problematic cases, which the published specification goes into in detail.
许多新行为基于只要有指向对象的强指针就保留对象,这与 Mac 上的垃圾收集非常相似.但是,技术基础却大相径庭.这种内存管理风格不是让垃圾收集器进程定期运行以清理不再指向的对象,而是依赖于我们在 Objective-C 中都需要遵守的严格的保留/释放规则.
Much of the new behavior, based on keeping objects around as long as there is a strong pointer to them, is very similar to garbage collection on the Mac. However, the technical underpinnings are very different. Rather than having a garbage collector process that runs at regular intervals to clean up objects no longer being pointed to, this style of memory management relies on the rigid retain / release rules we all need to obey in Objective-C.
ARC 只需将我们多年来必须执行的重复性内存管理任务卸载到编译器,这样我们就不必再担心它们了.这样,您就不会遇到在垃圾收集平台上遇到的停止问题或锯齿状内存配置文件.我在垃圾回收的 Mac 应用程序中体验过这两种情况,并渴望了解它们在 ARC 下的表现.
ARC simply takes the repetitive memory management tasks we've had to do for years and offloads them to the compiler so we never have to worry about them again. This way, you don't have the halting problems or sawtooth memory profiles experienced on garbage collected platforms. I've experienced both of these in my garbage collected Mac applications, and am eager to see how they behave under ARC.
有关垃圾收集与 ARC 的更多信息,请参阅 Chris Lattner 在 Objective-C 邮件列表中的这个非常有趣的回复,他在其中列出了 ARC 相对于 Objective-C 2.0 垃圾收集的许多优点.我遇到了他描述的几个 GC 问题.
For more on garbage collection vs. ARC, see this very interesting response by Chris Lattner on the Objective-C mailing list, where he lists many advantages of ARC over Objective-C 2.0 garbage collection. I've run into several of the GC issues he describes.
这篇关于Objective-C 中的自动引用计数不能防止或最小化什么样的泄漏?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Objective-C 中的自动引用计数不能防止或最小化什么样的泄漏?
基础教程推荐
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01