A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: A function that deletes an instance and replaces it with another keeps old one in mem

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    1

    A function that deletes an instance and replaces it with another keeps old one in mem

    I have a function that I use to delete an old instance by nulling it out, and afterwards, make a new instance in its place.

    I've made sure that inside my instance that all event listeners were weak, and I even removed them after being deleted by adding a function that detects when its removed from stage to remove all listeners anyways.

    Since that did not work, I tried just seeing what would happen if the instances had nothing in them at all, and it still keeps the items in memory. I detect this by having a weak linked event listener to trace some text while the code is active, and it keeps tracing even after I supposedly null the instance it is tracing from.

    Am I missing something? Is something out of order? Or is there a crucial step that I am overlooking? I've tried all tutorials I have come across to help fix the problem, and nothing seems to be working.

    The function that deletes the instances is below. It works by first deleting old references and then replaces them with new ones, then it nulls out the old instance and replaces it with a new one.

    Code:
    private function loadArea(inputArea:String)
                    {                      
                            //This is for a checker to make sure that areas only get loaded once.
                            currentRoom = inputArea;
                           
                            //If the area currently loaded is not null, make it null now.
                            if(selectedArea != null) selectedArea = null;
                           
                            //Null any data inside of the reference used to create the name of the new area.
                            areaReference = null;
                            //Grab the class using the input.
                            areaReference = getDefinitionByName(inputArea + "Area") as Class;
                           
                            //Null the sprite used to house the class
                            areaSprite = null;
                            //Set the holder as a new instance of the desired class.
                            areaSprite = new areaReference() as Sprite;
                                                                           
                            //If the selected area is still not null for some reason,
                            if(selectedArea != null)
                            {
                                    //Remove the area from the container...
                                    areaContainer.removeChild(selectedArea);
                                    //...and nullify it.
                                    selectedArea = null;
                            }
                           
                            //Set the current area as the newly created instance.
                            selectedArea = areaSprite;
                           
                            //If the area is not the "Game", load in the assets one way,                   
                            if(inputArea != "Game") selectedArea.construct(areaAssets);
                            //otherwise do it another way.
                            else selectedArea.construct(newScreenData,apiServer,cdnServer,areaAssets);
                           
                            //This is for a checker that fades out the screen, which it needs to fade back in soon.
                            newScreenData = null;
                           
                            //While the container for areas has any areas inside of it, remove them.
                            while(areaContainer.numChildren) areaContainer.removeChildAt(0);
                           
                            //...then add the new instance area to the container.
                            areaContainer.addChild(selectedArea);
                           
                            //...then let all the parts of the game know that a new area has been laoded in.
                            Global.echoEvent.echo("gameBootUp","playAreaIn");
                    }

  2. #2
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    After setting the object to null try System.gc(); which removes the content of the garbage collector.
    - The right of the People to create Flash movies shall not be infringed. -

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center