ARC的归零弱指针行为是如何实现的? [英] How does the ARC's zeroing weak pointer behavior implemented?

查看:64
本文介绍了ARC的归零弱指针行为是如何实现的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ARC.现在关于弱指针归零. 好的,我了解所有功能. 弱引用的语义与GC系统的弱引用相同,但是您知道,Objective-C不使用GC(特殊情况除外),因此我无法理解它的工作原理.

I'm studying ARC. And now about zeroing weak pointer. OK I understood all the features. The semantic of weak reference is just same with weak reference of GC system, but you know, Objective-C doesn't use GC (except special case) so I can't understand how this works.

我是一个有点复杂的人,所以我需要知道底层的实现主体才能接受要使用的功能.但是问题是很难找到描述弱指针归零的原理的文档:(

I'm a little complicated guy, so I need to know underlying implementation principal to accept the feature to use. But the problem is it's very hard to find document which describes the principal of zeroing-weak pointer :(

IMO,使此工作唯一的方法是在运行时跟踪并保持所有指针对其自身的引用,并在其引用计数变为0时将它们全部设置为nil.但这看起来太笨拙了.我相信实际的ARC实施中会使用很多更好的解决方案.

IMO, the only way to make this work is tracking and keeping all pointers referencing itself at runtime, and setting them all nil when its referencing count becomes 0. But this looks too heavy and stupid. I believe a lot better solution used in actual ARC implementation.

您能帮我找到文档吗?否则直接描述会更好!

Can you help me to find the documentation? Or direct description would be more great!

推荐答案

在这里说明:

http: //mikeash.com/pyblog/friday-qa-2010-07-16-zeroing-weak-references-in-objective-c.html

扰流器:

它的工作原理与您的想法差不多.每个类都维护着一组指向它的弱指针的地址,当调用它的dealloc时,会将它们全部设置为零.

It works pretty much how you'd think. Every class maintains a set of addresses of weak pointers that are pointing to it, and when its dealloc is called it sets them all to zero.

这似乎很愚蠢,但是速度很快,因为它不涉及跟踪",每次将对象分配给新的弱引用时,ARC都会插入代码以添加指向该集合的指针.

It may seem dumb, but it's fast because there's no "tracking" involved, ARC just inserts the code to add a pointer to that set every time the object is assigned to a new weak reference.

实际上,它比垃圾回收所涉及的欺骗要有效得多,后者基本上涉及在后台线程中遍历堆以查找指针并保留它们所指向的内容.

It's actually much more efficient than the trickery involved in Garbage collection, which basically involves wading through the heap on a background thread looking for pointers and keeping stock of what they're pointing to.

这篇关于ARC的归零弱指针行为是如何实现的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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