Optimization problem

hello,

I’m learning about basic memory optimization stuff and I have an example I need help with. This is extended Adobe documentation example on BaseButton class. You need Button component within Library in order for code to work.

import fl.controls.Button;
import fl.controls.Label;
import fl.events.ComponentEvent;
import flash.display.Shape;
import flash.display.Graphics;

var myLabel:Label = new Label();
myLabel.text = "0";
myLabel.move(10, 10);
addChild(myLabel);

var downButton:Button = new Button();
downButton.label = "-";
downButton.autoRepeat = true;
downButton.setSize(20, 20);
downButton.move(10, 30);
downButton.addEventListener(ComponentEvent.BUTTON_DOWN, buttonDownHandler);
addChild(downButton)

var upButton:Button = new Button();
upButton.label = "+";
upButton.autoRepeat = true;
upButton.setSize(20, 20);
upButton.move(40, 30);
upButton.addEventListener(ComponentEvent.BUTTON_DOWN, buttonDownHandler);
addChild(upButton);

var scale:Shape = new Shape();
scale.graphics.beginFill(0x00FF00);
scale.graphics.lineStyle(2, 0x00FF00);
scale.graphics.drawRect(10, 60, 100, 10);
scale.graphics.endFill();
addChild(scale);

var value:Number = Number(myLabel.text);

function buttonDownHandler(event:ComponentEvent):void {
    
    switch (event.currentTarget) {
        case downButton:
            if(value > 0) {
                value--;
            };
            break;
        case upButton:
            if(value < 100) {
                value++;
            }
            break;
    }
    updateScale(value);
    myLabel.text = value.toString();
}

function updateScale(val:Number):void {

    scale.graphics.beginFill(0x000000);
    scale.graphics.drawRect(10, 60, val, 10);
    scale.graphics.endFill();
    
}

When I’m testing this code, I hold + button until text value reaches 100 and then - button until text value reaches 0 and then I repeat this several times. After few cycles I notice there’s a memory problem, because I need more time to reach end of the scale.

My question is: why this is happening and how to solve this memory issue?

Thank you.