Would this cause memory leak ?
var mc:MovieClip ; //<<<<<<< OUTSIDE LOOP
for ( var i=0 ; i< 1000 ; i++)
{
mc = new MovieClip() ;
mc.addEventListener( MouseEvent.CLICK , onClick) ;
}
And what about this ?
for ( var i=0 ; i< 1000 ; i++)
{
var mc:MovieClip ; //<<<<<<< INSIDE LOOP
mc = new MovieClip() ;
mc.addEventListener( MouseEvent.CLICK , onClick) ;
}
“removeEventListener” is not used in any of the above code, so I think that both are causing a memory leak.
Your 1000 Movieclips will have a reference to you onClick function. Not the other way around.
So if you question is if your 1000 Movieclips will get GCed: They will eventually, if they don’t have any other reference.
On the other hand the reference in your movieclips to your onClick function will keep that alive (and the object it might belong to). If those MCs have any other reference that will keep them alive.
The following code:
Will have your listener function GCed pretty soon, as it doesn’t have any strong reference.
setting useWeakReference to true can be pretty relevant if your adding an Eventlistener to your stage
The above code will keep the Object with your listener Function alive, even if it has no other reference.
the above code will not keep your someObjectBelowIntheDisplayList alive. It has a reference to the stage, but the stage does not get a reference to someObjectBelowIntheDisplayList
Edit: Please try the following code:
This code clearly supports what I am saying: Using mc.addEventListener will not get the memory consumption up. It will stay around 20MB on my system.
When using the line with the stage.addEventListener and using mc.onClick as the listener function, memory consumption will rise every frame.