Actionscript 3: Do self event listeners prevent an object from being garbage collected?

前端 未结 2 345
轮回少年
轮回少年 2021-01-24 19:52

I know that event listeners and references to an object will prevent the garbage collector from dealing with objects. My question is, will an event listener placed on an object

2条回答
  •  一向
    一向 (楼主)
    2021-01-24 20:28

    Yes, this will stop the GC from cleaning up your object. A hacky way to try to prevent this is to use weak references when adding listeners.

     myobj.addEventListener(Event.EVENT, eventHandler, false, 0, true);
    

    The last true flag will set the listener to use a weak object reference.

    Best practice would be to keep track and always remove any active listeners before nulling your object.

    Check out this great blog post for more info on this topic

    http://gskinner.com/blog/archives/2006/06/as3_resource_ma.html

提交回复
热议问题