Marsh
Marsh

Reputation: 8145

AS3 - What happens when an object that is executing code is removed?

What happens when an object which has a function currently being executed has all its references removed?

I want to have a dialog box type object held in an array by the main class for my program, and when the dialog needs to be closed, I want it to be removed from the array during that close-screen function. My question is, assuming the dialog box object is in all other ways eligible for garbage collection, what happens to the code it's supposed to be executing?

Edit for clarification: The array is a layer of visual elements in my program, of which the dialog box is one. The idea is that the "OK" button (or whatever) that closes the box will also remove it from the array of objects being displayed at the same time.

Upvotes: 0

Views: 114

Answers (2)

laurent
laurent

Reputation: 90834

If all references to the object have been removed while a function of this object is being executed, the rest of the function will keep executing. When it's done, the object will be removed during the next garbage collection cycle.

Upvotes: 1

Marty
Marty

Reputation: 39466

Your object won't be eligible for garbage collection if there is something referencing it (in your case to call a method within it).

If you want to make your Dialogue Box eligible for garbage collection from within itself, you'll need to add a method that deals with self-removal from arrays that it may be within, etc.

Yours may look like this.

public function destroy():void
{
    var ix:int = someArray.indexOf(this);
    someArray.splice(ix, 1);

    if(parent)
        parent.removeChild(this);

    // ...remove event listeners, etc
}

Upvotes: 4

Related Questions