[AS3|Flex2] Variable usage and garbage collection

[quote=senocular;1918265]When ever there are no longer any references pointing to an object in memory, that object is purged from memory and permanently forgotten by the player.

   [FONT=Courier New][LEFT][COLOR=#0000ff]delete[/COLOR] a; [COLOR=#808080]*// Object has reference count of 1*[/COLOR]
[COLOR=#0000ff]delete[/COLOR] b; [COLOR=#808080]*// Object has reference count of 0, removed from memory *[/COLOR]
 [/LEFT]
[/FONT]

[FONT=Courier New][LEFT]
[/LEFT]
[/FONT][COLOR=Red][COLOR=Black]Note: remember, the delete operator only removes variable association, it does not delete objects in memory, the GC is responsible for that. Also[/COLOR] delete will only work on non class member variables.
[/COLOR] [/quote]
Thank You, i also have a question: I have a class called [COLOR=Blue]**MenuView **[/COLOR]which contains buttons. After a button from the menu is clicked, its content - which is contained in another class - is being attached to the existing class by the means of the [COLOR=Red]new [/COLOR]keyword, so basically it just creates SubClass inside the [COLOR=Blue]**MenuView **[/COLOR]class (or instance of another class inside the [COLOR=Blue]MenuView[/COLOR] Class object if you like):


_sign_content = **[COLOR=Red]new[/COLOR]** [COLOR=Blue]**NewEntryView**[/COLOR](width);
_sign_content.addEventListener("newEntry_submit", onNewEntrySubmit);
addChild(_sign_content);

[COLOR=DarkSlateGray]NOTE: _sign_content is private property of the class of type NewEntryView (this class contains the content for the button clicked)[/COLOR]

Inside this [COLOR=Blue]**NewEntryView **[/COLOR]Class (which is being referrenced by the _sign_content property by now), there are textFields being attached to the DisplayList, eventListeners being called, subclasses being created, all that in different functions of the class.

Now when user clicks another button in the menu, i have to set _sign_content=null etc., so that GC can harvest it.

I do so this way from within [COLOR=Blue]**MenuView **[/COLOR]class:


if(_sign_content != null) {
  // remove container
  removeChild(_sign_content);
  // remove variable reference of the object (obj will be removed by GC when there are no referrences on it)
  [COLOR=DarkGreen]**_sign_content**[/COLOR] = **null**;
  // remove listener
  removeEventListener("newEntry_submit", onNewEntrySubmit);
}

However, even after some time, if i **[COLOR=Black]had [/COLOR]**any classes declared in the [COLOR=Blue]**NewEntryView [/COLOR]class (referrenced by the [COLOR=DarkGreen]_sign_content **[COLOR=Black]variable[/COLOR][/COLOR]) - which should be now gone (?), or any Events running (for ex: TimerEvents - so that i can see they’re running in the output panel :P), these don’t get garbage-collected. Neither do variables etc.

So althought i removed all references to the [COLOR=Blue]**NewEntryView **[/COLOR]class instance (did i not?), its own variables, methods, listeners - seems [COLOR=Black]DO NOT[/COLOR] get removed (getting rid of the refference to the container doesn’t get rid of the contents of the container?). When the same menu button is clicked second time, i see duplicate TimerEvents being called along with the old ones, which i want to have destroyed (GC doesnt remove the old ones…).

I have following questions:
1.) Inside the NewEntryView class instance, is it recommended to have a method like …


private function removeContent():void {

    // removeChild/At on containers
    removeChild(_textField1);
    removeChild(_sprite2);
    ......
    
    // null on variables
    _tf = null;
    _spr = null;
    ......
    
    // .removeEventListener on event listeners (or weakRef)
    this.removeEventListener(TimerEvent.TIMER, onTimer);

    // anything else i forgot?
}

… and call it before removing the refference to the instance of NewEntryView ?
(kind of manual removal… first getting rid of all the inner pieces of the instance before destroying the shell… ), or is there an easier way of just removing the shell … and letting GC to take care of the rest?

2.) [COLOR=Red]EDIT:[/COLOR] figured out ([COLOR=Red]**
[*]**[/COLOR] read below)

If the answer to 1) is YES (dont rely on GC, remove everything manually), that basically means, that i have to keep the referrence to all the variables of the [COLOR=Blue]**NewEntryView **[/COLOR]private and available throughout the subclass, and i can’t do simply:


private function createTextField():void {
var tf:TextField = new TextField();
}

… because i’ll loose the reference to the TextField once i jump out of the function (and i can’t do removeChild(tf); later)…
Hmm… ok maybe it can be done other way…


private function createTextField():void {
var tf:TextField = new TextField();
     tf.name = "myTf";
}

....

// and later on
private function removeContent():void {
var tf:TextField = TextField(getChildByName("myTf"));
     removeChild(tf);
}

… but i am blabbing already, because i started guessing…

[COLOR=Red]**
[*]**[/COLOR] this is not necessary actually (oops i forgot!). Happily AS3.0 now keeps track of all the children objects, so to remove them from DisplayList, its enough to:


var nCh:uint = this.numChildren;
// trace("children: " + nCh);
for(var i:uint = 0; i < nCh; i++) {
  // trace(i, this.getChildAt(0).name);
  this.removeChildAt(0);
}

3) I am using Flex2.0. Anybody knows how to work with the Flex2 debugger? (i cant get the variables / objects …) displayed in the debug panel (?) :puzzle: It would be definitely easier to debug this thing without using trace :smiley:

Thank Your for Your time …
… and saying that, i like to hear You people take time for all the other more important things before devoting it to Your reply in this thread :wink: