Skip to main content
markerline
Inspiring
January 24, 2015
Answered

memory leak (addChild) as3

  • January 24, 2015
  • 1 reply
  • 1173 views

Hi, I have the following code:

import flash.events.MouseEvent;

import flash.display.Loader;

import flash.net.URLRequest;

import flash.display.MovieClip;

import flash.events.Event;

import flash.events.ProgressEvent;

var touchBox:TouchBox=new TouchBox();

var touchBoxArr:Array=new Array();

var mcArr:Array=new Array();

var tbNameArr:Array=new Array();

var cont:MovieClip=new MovieClip();

var mcCont:MovieClip=new MovieClip();

var mcLoader:Loader=new Loader();

cont.x=(3642-2048)/2;

cont.y=0;

addChild(cont);

function init():void{

    var kk:int=1;

    for (var i:int=0; i<34; i++){

        for (var j:int=0; j<18; j++){

            touchBox=new TouchBox();

//            touchBox.name=String(i)+String(j);

            touchBox.name=String(kk);

            touchBox.width=56*2;

            touchBox.height=56*2;

            touchBoxArr.push(touchBox);

            tbNameArr.push(kk);

            kk++;

            touchBox.x=(i+0)*touchBox.width;

            touchBox.y=j*touchBox.height;

           

            touchBox.addEventListener(MouseEvent.MOUSE_DOWN, onTouchBoxMouseDown);

            touchBox.addEventListener(MouseEvent.MOUSE_UP, onTouchBoxMouseUp);

            touchBox.addEventListener(MouseEvent.CLICK, onTouchBoxClick);

            touchBox.addEventListener(MouseEvent.ROLL_OVER, onTouchBoxRollOver);

            touchBox.addEventListener(MouseEvent.ROLL_OUT, onTouchBoxOut);

            addChild(touchBox);

//            trace(touchBox.name);

//            trace(touchBoxArr.length);

            trace(tbNameArr);

        }

    }

   

}

init();

init2();

function init2():void{

    for (var z:int=1; z<612; z++){

        if(z<10){

           

            mcLoader=new Loader();

            mcLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onComplete1);

            mcLoader.addEventListener(Event.COMPLETE, onDone1);

            mcLoader.addEventListener(Event.ENTER_FRAME, onComplete1a);

            mcLoader.load(new URLRequest("renamed_R_acdsee_8_ult/Houdini_FX_art_heals_images_prep.00"+z+".jpg"));

            mcArr.push(mcLoader);

           

        }

        if(z>=10&&z<100){

            mcLoader=new Loader();

            mcLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onComplete2);

            mcLoader.addEventListener(Event.COMPLETE, onDone2);

            mcLoader.addEventListener(Event.ENTER_FRAME, onComplete2a);

            mcLoader.load(new URLRequest("renamed_R_acdsee_8_ult/Houdini_FX_art_heals_images_prep.0"+z+".jpg"));

            mcArr.push(mcLoader);

           

           

        }

        if(z>=100&&z<611){

            mcLoader=new Loader();

            mcLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onComplete3);

            mcLoader.addEventListener(Event.COMPLETE, onDone3);

            mcLoader.addEventListener(Event.ENTER_FRAME, onComplete3a);

            mcLoader.load(new URLRequest("renamed_R_acdsee_8_ult/Houdini_FX_art_heals_images_prep."+z+".jpg"));

           

            mcArr.push(mcLoader);

           

        }

    }

}

function onDone1(e:Event):void{

    done1.text="complete";

}

function onDone2(e:Event):void{

    done2.text="complete";

}

function onDone3(e:Event):void{

    done3.text="complete";

}

function onComplete1(e:ProgressEvent):void{

    complete1.text=String(Math.round(100*(e.bytesLoaded/e.bytesTotal) ));

    trace("one "+Math.round(100*(e.bytesLoaded/e.bytesTotal)));

}

function onComplete2(e:ProgressEvent):void{

    complete2.text=String(Math.round(100*(e.bytesLoaded/e.bytesTotal) ));

    trace("two "+Math.round(100*(e.bytesLoaded/e.bytesTotal)));

}

function onComplete3(e:ProgressEvent):void{

    complete3.text=String(Math.round(100*(e.bytesLoaded/e.bytesTotal)));

    trace("three "+Math.round(100*(e.bytesLoaded/e.bytesTotal)));

}

function onComplete1a(e:Event):void{

    //complete1.text=String(100*(e.loaderInfo.bytesLoaded/e.loaderInfo.bytesTotal));

}

function onComplete2a(e:Event):void{

    //complete2.text=String(100*(e.loaderInfo.bytesLoaded/e.loaderInfo.bytesTotal));

}

function onComplete3a(e:Event):void{

    //complete3.text=String(100*(e.loaderInfo.bytesLoaded/e.loaderInfo.bytesTotal));

}

var myLoader:Loader=new Loader();

var noEvent:Boolean=true;

function onTouchBoxRollOver(e:MouseEvent):void{

    if(noEvent==false){

    trace(e);

            cont.addChild(myLoader);

        for (var k:int=1; k<611; k++){

            if(e.currentTarget.name==String(k)){

            if(k<10){

                trace(e.currentTarget.name);

                if(mcArr[k-1]){

                    //cont.removeChild(mcArr[k-1]);

                }

                cont.addChild(mcArr[k-1]);

            }

            if(k>=10&&k<100){

                if(mcArr[k-1]){

                    //cont.removeChild(mcArr[k-1]);

                }

                cont.addChild(mcArr[k-1]);

            }

            if(k>=100&&k<611){

       

                if(mcArr[k-1]){

                    //cont.removeChild(mcArr[k-1]);

                }

                cont.addChild(mcArr[k-1]);

            }

            cont.removeChildAt(0);

            }

        }

       

    }

}

function onTouchBoxMouseUp(e:MouseEvent):void{

    noEvent=true;

   

}

function onTouchBoxMouseDown(e:MouseEvent):void{

    noEvent=false;

   

}

function onTouchBoxOut(e:MouseEvent):void{

   

}

function onTouchBoxClick(e:MouseEvent):void{

   

                   

                    cont.addChild(myLoader);

                   

                for (var k1:int=1; k1<611; k1++){

                    if(e.currentTarget.name==String(k1)){

                   

                    if(k1<10){

                        if(mcArr[k1-1]){

                        //cont.removeChild(mcArr[k1-1]);

                        }

                        cont.addChild(mcArr[k1-1]);

                    }

                    if(k1>=10&&k1<100){

                        if(mcArr[k1-1]){

                            //cont.removeChild(mcArr[k1-1]);

                        }

                        cont.addChild(mcArr[k1-1]);

                    }

                    if(k1>=100&&k1<611){

                        if(mcArr[k1-1]){

                            //cont.removeChild(mcArr[k1-1]);

                        }

                        cont.addChild(mcArr[k1-1]);

                    }

                    }

                    cont.removeChildAt(0);

                }

}

/////////////////////////////

I am trying to remove the appropriate display objects so that they don't accumulate in system memory as they are currently doing.  However I do not know how to do this.  Every time I create a new addChild object by clicking or dragging with the mouse over a grid of buttons these children get added to the display list but the hidden ones remain in memory, thus accumulating memory and running into the problem of Flash Player running out of memory, even in a 64-bit system with lots of memory.

Any suggestions on how to properly do a removeChild or removeChildAt ?

Thanks in advance.

This topic has been closed for replies.
Correct answer markerline

I have found a solution, though not a proper one.  The solution I would like is to correctly use the removeChild or removeChildAt method(s).

What I did instead was to

1.  addChild'ed all of the loaded images to the display list

2.  set the added Childrens' display visibility property to false

3.  listen for the even that would trigger the visibility of the given child to be true

4.  set all of the children in the array i created to have a visibility of false in the for loop

5.  set only the currently activated array element's visibility to true, which displays only the correct image and does not overlap the same image more than once into memory.

Hopefully someone can find my solution useful.  I can post code if necessary.

1 reply

markerline
markerlineAuthorCorrect answer
Inspiring
January 24, 2015

I have found a solution, though not a proper one.  The solution I would like is to correctly use the removeChild or removeChildAt method(s).

What I did instead was to

1.  addChild'ed all of the loaded images to the display list

2.  set the added Childrens' display visibility property to false

3.  listen for the even that would trigger the visibility of the given child to be true

4.  set all of the children in the array i created to have a visibility of false in the for loop

5.  set only the currently activated array element's visibility to true, which displays only the correct image and does not overlap the same image more than once into memory.

Hopefully someone can find my solution useful.  I can post code if necessary.

Inspiring
January 28, 2015

What I always do is make container sprites. You then container.addChild(myThing) and then later it's very easy to empty your containers like:

while(container.numChildren){

     container.removeChildAt(0);

}

markerline
Inspiring
January 29, 2015

Thanks dmennenoh.  I will try that approach when I am able to do so.