弱引用 addEventListener
2010年6月27日
没有评论
之前只是知道尽量用强引用,不要用弱引用,因为弱引用会被垃圾回收.
对于flash.utils.Dictionary倒是没有什么疑问.但EventDispatcher.addEventListener就不明白具体是侦听器被回收了,还是侦听者被回收了 ![]()
昨天有看了一些文章,又想起这个问题,就想要理解透彻,于是又仔细看了下API文档.
Class-level member functions are not subject to garbage collection, so you can set useWeakReference to true for class-level member functions without subjecting them to garbage collection. If you set useWeakReference to true for a listener that is a nested inner function, the function will be garbge-collected and no longer persistent. If you create references to the inner function (save it in another variable) then it is not garbage-collected and stays persistent.
原来这里的弱引用是对侦听器的(侦听器函数),跟侦听者没有关系.
为了验证这个观点,我做了个小Demo.
OK,让我们来一边享受引用,一边学习吧 ![]()
阅读全文...