Objetcs created inside a function are automatically marked for garbage collection if not referenced anywhere else?
Let´s say, I have a class called SubClass. in the constructor I create some displayObjects. Then I instatiate SubClass somewhere. When开发者_开发百科 I remove this SubClass instance, will the objects inside be marked for garbage collection?
thanks in advance! cheers Bruno
Yes, unless you have references to SubClass's members anywhere outside SubClass, or you keep an active reference to the outside of your class from within SubClass (or any objects within it).
A typical example of the latter is if SubClass subscribes to a Stage event; if the listener is not weak (5th argument of addEventListener) you will keep an active link between the stage and your SubClass instance, and even if you remove the object and null it, it will not be collected. For simple listeners you can set that 5th argument to true, so the reference is weak and will be broken by the garbage collector. For more complex situations (or for example NetStreams, Loaders, Timeline audio, etc...), you need to create a way for the class to unlink itself for any outside objects and stop any process that could prevent collection, like a public destroy() method that closes requests, stopping media, removeListeners, etc...
But then again, for simple situations when you only have isolated childs, and no references to the outside of your class, simply removing your instance and nulling its reference should be enough for the garbage collection.
精彩评论