Creating a destroy function

Always implement a function – called destroy() – on every object that you create, and call it every time you finish working with an object. The function contains all the code necessary to remove all references to and from the object (excluding the reference which was used to call the function), so you make sure the object leaves your application totally isolated, and is easily recognized by the GC.

// Create this in every object you use
public function destroy():void
    // Remove event listeners
    // Remove anything in the display list
    // Clear the references to other objects, so it gets totally isolated
// ...
// When you want to remove the object, do this:
// And then null the last reference to it
theObject = null;
In this function, you’ll have to clear everything from the object, so it remains isolated in the application. After doing that, it will be easier for the GC to localize and remove the object.

Post A Comment

Anti-Spam Quiz: