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.
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...
//...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.
//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.
//...then add the new instance area to the container.
//...then let all the parts of the game know that a new area has been laoded in.
After setting the object to null try System.gc(); which removes the content of the garbage collector.
Click Here to Expand Forum to Full Width