在关闭模块之前删除事件侦听器

时间:2021-01-18 00:02:45

I add an instance of this module to the dispay list and then remove it. Will the event listener attached to foo create a leak? A simplified module:

我将此模块的实例添加到dispay列表中,然后将其删除。附加到foo的事件监听器是否会产生泄漏?一个简化的模块:

<s:Module creationComplete="init()">
    <fx:Script>
        protected var foo:Bar = new Bar();
        protected function init() {
          foo.addEventListener(MouseEvent.CLICK, foo_clickHandler);
        }

        protected function foo_clickHandler(event:MouseEvent):void {
            //do something
        }
    </fx:Script>
</s:Module>

2 个解决方案

#1


1  

This module does not have memory leaks since you add listener to a local object, not to a Stage, StyleManager or some other global-accessible stuff.

此模块没有内存泄漏,因为您将侦听器添加到本地对象,而不是舞台,StyleManager或其他一些全局可访问的东西。

#2


0  

It will. You should remove the listener, or use weak references when creating the listener.

它会。您应该删除侦听器,或在创建侦听器时使用弱引用。

foo.addEventListener(MouseEvent.CLICK, foo_clickHandler, false, 0, true);

#1


1  

This module does not have memory leaks since you add listener to a local object, not to a Stage, StyleManager or some other global-accessible stuff.

此模块没有内存泄漏,因为您将侦听器添加到本地对象,而不是舞台,StyleManager或其他一些全局可访问的东西。

#2


0  

It will. You should remove the listener, or use weak references when creating the listener.

它会。您应该删除侦听器,或在创建侦听器时使用弱引用。

foo.addEventListener(MouseEvent.CLICK, foo_clickHandler, false, 0, true);