PDA

View Full Version : Destroying instances



Miguelele
January 4th, 2008, 11:30 AM
I have a class that displays a picture in a popup style.
var popPicture:PopPicture = new PopPicture To remove it I use
removeChild and to remove completely the instance I pass
popPicture=null

But doing debugging I have discovered that I can trace the name of the instance using
popPicture.name and every time I create a new instance of the popup, the name is increased instance25, instance27, instance28.

I am not sure if it is just an internal counter that do no reuse numbers o if the instances are not really deleted. Can someone tell me if this is the ritght way to delete instances?

Thanks

NCMentis
January 4th, 2008, 12:28 PM
I'm working on the same thing, writing this will help me get my head around it too. As I see it there are three ways of removing instances

the first is within the scope of the object to use

removeChild(myobject);

you will find if you try and reference this from another function that you will get compiler errors as the name is no longer current

so, secondly - you can also use

removeChildAt(index) where you specify the depth of the object to be removed - this is a zero referenced index - meaning 0 is the first object on the display list, so I often use removeChildAt(numChildren-1) which removes the last object you want to remove is added to the display list. This is extremely useful ,but you cant always be sure the object you want to remove is the one at the top of the list. In those circumstances you can do one of two things (Im sure there are more ways - but Im only a couple of months into this language) the first way is heavy handed, but it has its uses - set up a loop using numchildren - this is one way I learned from ActionScript Cookbook(recommended)

var count:int = container.numChildren;
for ( var i:int = 0; i < count; i++){
container.removeChildAt(0);
}

then when this is done I call my function that redraws the stage the way I want it - like I said heavy handed but it works

one key thing to remember when using the child index is that if you addChild repeatedly for the same object, it does not create new instances on the display list. What it does, as I understand is to move the object from its current position to the top of the display list.

As I trace this in my code I find the instance name also changes,

so the third approach is to remove using instance name using

removeChild(getChildByName("instanceName"))

this requires that you either set the name explicitly by saying myObject.name = "fred" - or store the instance name somewhere, and critically remember to keep it up to date by calling this logic with an event listener that listens for added, removed and also render events, because what I see is that the instance name seems to change; so if I repeatedly addChild(myobject) and trace(myobject.name) I get a series of values of instancen where n is some number. If I want to be very specific about what I want to remove, then I have to keep track of the name value somewhere that is accessible from the function I want to use to remove the object and then use that value in a removeChild(getChildByName("instanceName")) statement.

Maybe some of the real gurus will chime in on this -

anyway my job this am is to implement something like this - so thanks for giving me the opportunity to write some notes to clarify my thoughts on the topic!

Miguelele
January 4th, 2008, 01:29 PM
As far as I know, using removeChild only removes the "displayed" information, not the object itself, that remains in memory…

Krilnon
January 4th, 2008, 02:03 PM
Setting all references to popPicture to null is the correct way to make it a candidate for garbage collection. You are probably correct about the automatically generated instance names.

Gathan
January 4th, 2008, 05:27 PM
As far as I know, using removeChild only removes the "displayed" information, not the object itself, that remains in memory…
That's how it works on AS 3.0, it allows you to move objects from one parent to another, dynamically.
Which wasn't possible in previous versions. You must null all references to the object, so the GC will remove it from memory when it does a sweep.
There is noway to tell the GC to remove it from memory if your wondering.