AS3内存泄漏示例 [英] AS3 Memory Leak Example

查看:443
本文介绍了AS3内存泄漏示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人张贴了的AS3 code(具体事件侦听器在内),这将是对一些可能出现内存泄漏......也希望你能张贴解决显示问题的一个简单的例子,一个例子吗?

现在的问题是:什么是一个AS3事件侦听器泄漏内存以及如何解决这个问题的一个简单例子

解决方案

 公共MySprite的类扩展Sprite {

    公共MySprite的函数(){
        如果(阶段){
            在里面();
        } 其他 {
            的addEventListener(Event.ADDED_TO_STAGE,INIT);
        }
    }

    私有函数初始化(E:事件= NULL):无效{
        stage.addEventListener(Event.RESIZE,handleStageResize);
    }

    私有函数handleStageResize(五:事件):无效{
        //做一些处理在这里。
    }

}
 

别的地方:

  MySprite的变种:= MySprite的MySprite的新();
someHolder.addChild(MySprite的);
 

现在,如果在以后的某个时候,你MySprite的删除,它仍然流连在内存中,因为它增加了本身(或引用本身)的阶段,在init()方法。

在这种情况下,最好的办法,以避免这种情况,可以删除添加到舞台监听MySprite的时候从显示列表中删除。

 私有函数初始化(E:事件= NULL):无效{
        的addEventListener(Event.REMOVED_FROM_STAGE,清理);
        stage.addEventListener(Event.RESIZE,handleStageResize);

    }

    私有函数清理(五:事件):无效{
       stage.removeEventListener(Event.RESIZE,handleStageResize);
    }
 

我相信其他人会告诉你添加监听到舞台时使用弱引用,但是你无论如何应该删除你的听众。如果不这样做,当你从显示列表中删除,并MySprite的没有其他裁判给它,将有资格获得GC和最终会被抹去的记忆。但是,直到出现这种情况,在handleStageResize(在code)将继续执行。

Can someone post an example of as3 code (specifically event listener included) that would be a simple example of something that could leak memory... also hopefully could you post a solution to the problem shown?

The question is: What is a simple example of leaking memory in an AS3 event listener and how can you solve it?

解决方案

public class MySprite extends Sprite {

    public function MySprite() {
        if(stage) {
            init();
        } else {
            addEventListener(Event.ADDED_TO_STAGE,init);
        }
    } 

    private function init(e:Event = null):void {
        stage.addEventListener(Event.RESIZE,handleStageResize);
    }

    private function handleStageResize(e:Event):void {
        //  do some processing here.
    }

}

Somewhere else:

var mySprite:MySprite = new MySprite();
someHolder.addChild(mySprite);

Now, if at some later point you remove mySprite, it'll still hang around in memory, because it has added itself (or a reference to itself) to the stage in the init() method.

In this scenario, the best way to avoid this could be removing the listener added to the stage when mySprite is removed from the display list.

    private function init(e:Event = null):void {
        addEventListener(Event.REMOVED_FROM_STAGE,cleanUp);
        stage.addEventListener(Event.RESIZE,handleStageResize);

    }

    private function cleanUp(e:Event):void {
       stage.removeEventListener(Event.RESIZE,handleStageResize); 
    }

I'm sure other people will tell you to use weak references when adding the listener to the stage, but you should remove your listeners anyway. If you don't, when you remove mySprite from the display list and have no other refs to it, will be eligible for GC and will eventually be wiped away from memory. But until that happens, the code in handleStageResize() will continue to execute.

这篇关于AS3内存泄漏示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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