Squirrel

The programming language
Welcome to Squirrel Sign in | Join | Help
in Search

Squirrel/SqPlus crash

Last post 07-06-2008, 1:57 PM by ats. 3 replies.
Sort Posts: Previous Next
  •  07-06-2008, 4:31 AM 2614

    Squirrel/SqPlus crash

    I've encountered a problem that crashes my program. Here's some example code:

    in C++:
    --------

    class CGameObject {
    public:
        int x;
        CGameObject() { x = 120; };
    };

    SQClassDef <CGameObject> (_SC("CGameObject"))
        .var(&CGameObject::x, _SC("x"))
        ;


    in Squirrel:
    -------------

    class Test extends CGameObject {
    }

    local tester = Test();
    print("x= "+tester.x+"\n");

    This prints the value of x which is 120. But if I change the class "Test" to have a constructor:

    class Test extends CGameObject {
        constructor () {
        }
    }

    local tester = Test();
    print("x= "+tester.x+"\n");

    The last line crashes my program. Am I doing anything wrong? This happens no matter if a constructor is declared in "CGameObject" (in C++) or not.
  •  07-06-2008, 6:09 AM 2615 in reply to 2614

    Re: Squirrel/SqPlus crash

    I just found out that if I change the "Test" class like this:

    class Test extends CGameObject {
        constructor () {
           CGameObject.constructor();
        }
    }

    The following two lines do not cause the crash.

    local tester = Test();

    print("x= "+tester.x+"\n");


    Although this seems to work it's not logical to me, as I can clearly see that the C++ constructor gets called anyway since x has the value 120. Might this be a bug or just something I don't understand? :)

    edit: Does SqPlus automatically generate a constructor if I do not declare one myself in "Test"? And does that auto-generated ctor call the base-class-ctor? If it was like that I would understand what was going on^^
  •  07-06-2008, 6:46 AM 2616 in reply to 2615

    Re: Squirrel/SqPlus crash

    It is normal that if you override an contructor in a derived class you must call the base constructor explicitly. The fact that sqplus crashes otherwise, is probably because they don't want to put a check for each method call to see if an object has been properly constructed. Bottom line, is not a bug. Maybe the SQPlus guys could put an assertion somewhere that is only compiled in debug mode or somthing similar, so that this issues are easier to pindown.

    Alberto

  •  07-06-2008, 1:57 PM 2618 in reply to 2616

    Re: Squirrel/SqPlus crash

    I've been stepping through this now and I'm a bit confused over it.

      print("x= "+tester.x+"\n");

    SqPlus does a sq_getobjtype(...) on the instance when trying to resolve the
    var (it expects a custom type tag).. But it gets a NULL pointer in return.
    So it throws a SquirrelException.

    In a sense, it's true, the Squirrel derived class has no type tag. Usually we
    get the type tag of the base class then. It seems that this is the connection
    that has never been made (and that usually happens on Squirrel side).

    Strange effect of not manually invoking the constructor (and not one could
    anticipate).

    Regards
    // ATS.

View as RSS news feed in XML
Powered by Community Server, by Telligent Systems