I have a bug that has taken a giant amount of time to track down, and I'm thinking it 'might' come down to the fact that SetInterval doesn't seem to take anonymous functions. Hmm, well I'm not actually using anonymous functions, but it is the only behaviour that seems to be similar to what I'm (not) seeing, so I'm pursuing that at the moment...

This works, of course:

var cls = function(){}
cls.prototype.meth = function(){trace("call")};
inst = new cls();
setInterval(inst, "meth", 500);

And this does not work:

var cls = function(){}
cls.prototype.meth = function(){trace("call")};
setInterval(new cls(), "meth", 500);

Is this a known behaviour? I'd love any more info on this as to what is happening, and what is causing it to fail...

I have a point in the code where I have an obj, meth, and interval count (obj, string and int), I can call the method from there using obj[meth](). With that and traces, type check etc, I'm pretty sure they are valid. Then if I setInterval with them, it returns an interval id, but the method never gets called. If I use a static method (with no method name, the other overload of setInterval) it works. I have also been thinking it could be somehow not getting that overload properly, but the types seem right.

<Compliant> That is a really crazy overload imo, one is setInterval(object, int, args..) the other is setInterval( object, string, int, args...). Given the way swf freely converts between these types, it is very hard to nail down what is happening inside setInterval. </Compliant>

The third possibilty I've been looking at is that I'm using setInterval.apply(null, args) - at the bytecode level setInterval requires CallFunction, maybe it is somehow getting mixed up with this apply. I can use 'apply'from the flash IDE though, and can't seem to generate the same problem I'm seeing.

Last thing I'm thinking, (well first, just I've been over and over it : ) is I'm just doing somehting plain and simple wrong, and missed it. This is all from generated bytecode, but here is the reversed code from ASV (oh, love that program!):

_global._setInterval = function (delg, time)
{
 var d = delg._delegates[0];
 var args = [];
 args.push(d.obj);
 if (d.name != null)
 {
     args.push(d.name);
 }
 var i = 1;
 while (i < arguments.length)
 {
     args.push(arguments[i]);
     i++;
 }
 // TEST
 args[0][args[1]](); // calls the method on the instance, no problem
 trace (args[0]); // traces the instance object as [object], np
 setInterval(args[0], "CheckVarsLoaded_9", 1000); // doesn't work

 // also doesn't work (though rets interval ID)
 return (setInterval.apply(null, args));
}

Any insights, similar issues, theories, legends, commentary, or outrageous comments you may have would be dearly appreciated : ).

Thanks!

posted on Monday, February 07, 2005 1:34 PM
Feedback
  • # RE: Is this a bug in SetInterval (swf)?
    Robin Debreuil
    Posted @ 2/7/2005 3:11 PM
    ok, think I have it... at some point the ref to that class is getting destroyed. The failing code (in C#) looks something like:
    public static void Main()
    {
    InstLoadVars ilv = new InstLoadVars();
    }
    and working code looks like:

    public static InstLoadVars ilv;
    public static void Main()
    {
    ilv = new InstLoadVars();
    }

    I'm thinking the main method is called like:
    main();
    where it might just all start working if it is
    var m = main();

    here is 'the post that helped the most' (thank you from me too, senocular : ):

    http://www.actionscript.org/forums/archive/index.php3/t-53594.html


  • # RE: Is this a bug in SetInterval (swf)?
    Bob
    Posted @ 2/7/2005 3:17 PM
    My guess is that setInterval needs a stored object instance for calling any methods on. Altough it's really just a guick guess ;).



  • # RE: Is this a bug in SetInterval (swf)?
    Cort
    Posted @ 2/7/2005 3:17 PM
    Hi Robin,

    I don't know if this helps or not but I think what it is doing is that the object is getting defined in some temporary scope.

    If you do this it will work.

    _global.cls = function(){}
    cls.prototype.meth = function(){trace("call")};
    setInterval(_global.c = new _global.cls(), "meth", 500);

    Hope that's usefull in some way or other

    -Cort

  • # RE: Is this a bug in SetInterval (swf)?
    Cort
    Posted @ 2/7/2005 3:19 PM
    Whups... Always refresh before posting.

    Looks like you already found it :D

  • # RE: Is this a bug in SetInterval (swf)?
    Greg Burch
    Posted @ 2/7/2005 3:44 PM
    Seems like a bug to file on the wishform. The underlying c code obvisouly doesn't maintain its reference so the object gets garbage collected.

  • # RE: Is this a bug in SetInterval (swf)?
    Robin Debreuil
    Posted @ 2/7/2005 4:03 PM
    thanks a ton for the help - that is the problem for sure. I think it comes down to the Main method we are using, where we (were) allowing just informally creating the application, like:

    public static void Main()
    {
    App a = new App();
    }

    We (ok I, my bug!) will need to put in a more formal startup process, maybe like in C# -

    App frm = new App();
    Application.Run(frm);

    which will translate to something that holds the app process, like:

    public static App frm;
    ...
    frm = new App();

    The worst thing is I have always used a Main method like this in actionscript, so I went back and checked a few scripts. Urgh, I guess I knew this at one point because they all save the app process somewhere :(.

    Thanks again for all the quality and instant help, I promise not to abuse the privilege of having people such as yourselves close by... Well not too much anyway : ).

    Much appreciated,
    Robin

  • # RE: Is this a bug in SetInterval (swf)?
    Freddy
    Posted @ 2/7/2005 8:51 PM
    One question as i'm not a master at AS...

    If you use your not working example:

    var cls = function(){}
    cls.prototype.meth = function(){trace("call")};
    setInterval(new cls(), "meth", 500);

    for the sake of the argument let's assume its working...

    how would you clear the interval as it is not instanciated?
    If you do this at root:

    var cls = function(){}
    cls.prototype.meth = function(){trace("call")};
    new cls();

    test movie and do a "show variables", there is no object at all...

    Variable _level0.cls = [function] {
    prototype:[object #2, class 'Object'] {
    meth:[function 'meth']
    }
    }

    but if you do this:

    var test = new cls();

    when "show variables" you get:

    Variable _level0.cls = [function] {
    prototype:[object #2, class 'Object'] {
    meth:[function 'meth']
    }
    }
    Variable _level0.test = [object #4] {}
    there is your object to assign to the interval...


  • # RE: Is this a bug in SetInterval (swf)?
    Robin Debreuil
    Posted @ 2/7/2005 9:06 PM
    Hi Freddy,

    I was assuming passing an anonymous function to setInterval would be enough to create a reference to it, and this would allow it to still exist after the calling method fell out of scope (and thus not be garbage collected). To clear it, had that worked of course, you could still use the returned index, as it doesn't need an instace of the object to clear it. I guess that is a clue that it isn't hanging on to an instance reference though (I assume because it is native code), the fact that you clear it with an integer rather than an object reference.

    Of course you are right in the end, it does need an instance to exist (and thus a reference to it other than the one it is(not) holding). I can see why it would need to work like this, though I think it might have been better and bite the bullet and have better object tracking between the native code and the AS. That would also allow clearing intervals using the actual inst/method reference rather than a number...

    Oh well, just glad I finally know what the problem is : ).

  • # RE: Is this a bug in SetInterval (swf)?
    Freddy
    Posted @ 2/9/2005 1:06 PM
    hehe, now I know a little more about the really really deep AS stuff(native code is not my favorite candy) :), thanks Robin!!

Blog Stats

  • Posts - 121
  • Stories - 1
  • Comments - 1441
  • Trackbacks - 47

.Net Blogs

01101 Blogs

Flash Blogs

Graphics

People