views:

50

answers:

2

Hi, I'm having problems with some AS3 code that simply draws on a Sprite's Graphics object. The drawing happens as part of a larger procedure called on every ENTER_FRAME event of the stage.

Flash neither crashes nor returns an error. Instead, it starts running at 100% CPU and grabs all the memory that it can, until I kill the process manually or my computer buckles under the pressure when it gets up to around 2-3 GB. This will happen at a random time, and without any noticiple slowdown beforehand.

WTF? Has anyone seen anything like this?

PS: I used to do the drawing within a MOUSE_MOVE event handler, which brought this problem on even faster.

PPS: I'm developing on Linux, but reproduced the same problem on Windows.

UPDATE: You asked for some code, so here we are. The drawing function looks like this:

public static function drawDashedLine(i_graphics : Graphics, i_from : Point, i_to : Point, i_on : Number, i_off : Number) : void
{
    const vecLength : Number = Point.distance(i_from, i_to);

    i_graphics.moveTo(i_from.x, i_from.y);

    var dist : Number = 0;
    var lineIsOn : Boolean = true;
    while(dist < vecLength)
    {
        dist = Math.min(vecLength, dist + (lineIsOn ? i_on : i_off));
        const p : Point = Point.interpolate(i_from, i_to, 1 - dist / vecLength);
        if(lineIsOn) i_graphics.lineTo(p.x, p.y);
        else i_graphics.moveTo(p.x, p.y);

        lineIsOn = !lineIsOn;
    }
}

and is called like this (m_graphicsLayer is a Sprite):

m_graphicsLayer.graphics.clear();
if (m_destinationPoint)
{
    m_graphicsLayer.graphics.lineStyle(2, m_fixedAim ? 0xff0000 : 0x333333, 1);
    drawDashedLine(m_graphicsLayer.graphics, m_initialPos, m_destinationPoint, 10, 10);
}
A: 

Well, CookieOfFortune was right- there was an infinite loop in the case that one of the end points was at infinity (which wasn't supposed to happen ;)

Thanks for your help.

drpepper
A: 

while(dist < vecLength)

This should always jump out as a red flag during debugging and code review, especially if the arguments come from a calculation which you can't reliably predict when writing code, or the values don't have a strict upper limit (like Point.distance() or Math.min()).

Been bitten by those a number of times. :-)

jpop
You're right. Still though, why would Flash the way it does, grabbing all this memory and never releasing it?
drpepper
Probably because the method is stuck in a infinite loop which allocates more resources with each iteration, and never releases them back. It just keeps going until the process breaks down.
jpop