js crashes max - what's the problem with this code?

Myer's icon

i am building a simple template to create a grid of objects based on numbers passed in. it works perfectly the first time, but the second time through it crashes max. as far as i can tell there are no infinite loops. am i missing something? i'm running XP.
myer

Max Patch
Copy patch and select New From Clipboard in Max.

here's my patch
***********************************************

// variables
var oldelements = 0;
var theelements = new Array(128);

// methods

function msg_int(a, b, c, d) // accept int input as calling the slider function
{
    grid(a, b, c, d)
}

// generate ctlins
function grid(xelements, yelements, deltax, deltay)
{

    // safety check for number of sliders
    if(oldelements
        {
        oldelements = 0; // too few sliders, set to 0
        }
    if(oldelements>128)
        {
        oldelements = 128; // too many sliders, set to 128
        }

    // take out the existing ones
    for (var i=0;i
    {
        this.patcher.remove(theelements[i]);
        post(i);
    }

    // re-assign the "old element" value
    oldelements = (xelements)*(yelements);
    post(oldelements);

    // put in the new ones
    for (var j = 0; j
    {
        for (var k = 0; k
        {
            theelements[k] = this.patcher.newdefault((150+(k*(deltax))), (100+(j*(deltay))), "ctlin");
        }
    }
}

***********************************************************

pdelges's icon
lists@lowfrequency.or's icon

the only crashes I've seen using javascript result from calling
patcher.remove() on max objects that don't actually exist - check the
manual, there's a function (property? I don't remember exactly which
it is right now) called something like isValid that makes sure that
you can properly call remove without crashing Max.

best
evan

On Aug 2, 2006, at 3:48 AM, Myer wrote:

>
> i am building a simple template to create a grid of objects based
> on numbers passed in. it works perfectly the first time, but the
> second time through it crashes max. as far as i can tell there are
> no infinite loops. am i missing something? i'm running XP.
> myer
>
> here's my patch
> ***********************************************
> max v2;
> #N vpatcher 38 383 789 956;
> #P origin 11 0;
> #P window setfont "Sans Serif" 10.;
> #P window linecount 1;
> #P message 107 272 53 9109514 5 4 90 90;
> #P message 27 274 53 9109514 4 4 50 50;
> #P newex 21 363 100 9109514 js sliderexample2.js;
> #P message 19 394 97 9109514 sliders $1 $2 $3 $4;
> #P connect 2 0 1 0;
> #P connect 3 0 1 0;
> #P pop;
> **************************************************
> here's me script:
> *********************************************************
>
> // ctlin.js
> //
> // pass in numbers and get a specified grid of ctlin objects
> //
>
> // inlets and outlets
> inlets = 1;
> outlets = 0;
>
> // variables
> var oldelements = 0;
> var theelements = new Array(128);
>
> // methods
>
> function msg_int(a, b, c, d) // accept int input as calling the
> slider function
> {
>     grid(a, b, c, d)
> }
>
> // generate ctlins
> function grid(xelements, yelements, deltax, deltay)
> {
>
>     // safety check for number of sliders
>     if(oldelements
>         {
>         oldelements = 0; // too few sliders, set to 0
>         }
>     if(oldelements>128)
>         {
>         oldelements = 128; // too many sliders, set to 128
>         }
>
>     // take out the existing ones
>     for (var i=0;i
>     {
>         this.patcher.remove(theelements[i]);
>         post(i);
>     }
>
>     // re-assign the "old element" value
>     oldelements = (xelements)*(yelements);
>     post(oldelements);
>
>     // put in the new ones
>     for (var j = 0; j
>     {
>         for (var k = 0; k
>         {
>             theelements[k] = this.patcher.newdefault((150+(k*(deltax))), (100
> +(j*(deltay))), "ctlin");
>         }
>     }
> }
>
>
>
>
> ***********************************************************
>

Myer's icon

thanks guys - this helps a lot. when i'm done with my object (which will help streamline midi implementation into new patches by routing inputs to a predictable nomenclature - button row 1 number 1 (br1n1, etc), i'll post it). :)
myer