Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 4

Qt-interest Archive, June 2007
QtScript: adding function properties to global object


Message 1 in thread

What I am trying to do is add some helper functions to the global
object. However with certain property names the program crashes while
deleting the engine. For example when I set the properties 'log',
'print' or 'test' all works. If I however set other properties for
example with the names 'lerror' or 'perror', the crash in the engine
destructor happens.

I have reduced it to the small as possible testcase below. When run as
it is, all is ok. When one or both lines of perror or lerror are
uncomented, it crashes.

I am using Qt commercial 4.3.0 on windows with visual studio 2005.

main.c:

#include <QCoreApplication>
#include <QtDebug>
#include <QScriptEngine>
#include <QStringList>

static QScriptValue f(QScriptContext* c, QScriptEngine* e)
{
        qDebug() << c->callee().toString();
        return QScriptValue();
}

int main(int argc, char *argv[])
{
        QCoreApplication a(argc, argv);
        QScriptEngine e;
        e.globalObject().setProperty("log", e.newFunction(f));
        e.globalObject().setProperty("print", e.newFunction(f));
        e.globalObject().setProperty("test", e.newFunction(f));
        //e.globalObject().setProperty("lerror", e.newFunction(f));
        //e.globalObject().setProperty("perror", e.newFunction(f));
        QScriptValue r = e.evaluate(
                "print('x');\n"
                "log('x');\n"
                "test('x');\n"
        );
        if (!r.isUndefined()) qDebug() << r.toString();
        if (e.hasUncaughtException()) {
                QStringList b = e.uncaughtExceptionBacktrace();
                qDebug() << b.join("\n");
        }
}

--
 [ signature omitted ] 

Message 2 in thread

While further experimenting with QtScript I see that this problem is
not only with functions, but can raise with any property added to the
global object. Also this second sample will cause a crash in the heap
deallocation at program end. I tried qt 4.3.0 binary commercial
distribution as well as 4.3.1 snapshot 18.6.2007, both with
Visual Studio 2005 SP1.

Can anybody confirm this, or point out where the mistake could be at
my side, please?


// --- main.cpp ---

#include <QtDebug>
#include <QCoreApplication>
#include <QScriptEngine>
#include <QStringList>
#include "myclass.h"

QScriptValue ctor_MyClass(QScriptContext* context, QScriptEngine* engine)
{
        return engine->newQObject(new MyClass, QScriptEngine::ScriptOwnership);
} // ctor_MyClass

int main(int argc, char *argv[])
{
        QCoreApplication a(argc, argv);
        QScriptEngine e;
        e.globalObject().setProperty("MyClass", e.newFunction(ctor_MyClass));
        QScriptValue r = e.evaluate(
                "var m = new MyClass();\n"
                "m.text = 'hello';\n"
                "m.append(' world');\n"
                "print(m.text);\n"
        );
        if (!r.isUndefined()) qDebug() << r.toString();
        if (e.hasUncaughtException()) {
                QStringList b = e.uncaughtExceptionBacktrace();
                qDebug() << b.join("\n");
        }
} // main

// --- myclass.h ---

#ifndef MYCLASS_H
#define MYCLASS_H

#include <QObject>
#include <QString>
#include <QScriptable>

class MyClass : public QObject, protected QScriptable
{
        Q_OBJECT
        Q_PROPERTY(QString text WRITE setText READ text)

public:
        MyClass(QObject *parent = 0);
        ~MyClass();

public Q_SLOTS:
        QString text() const;
        void setText(const QString& text);
        void append(const QString& text);

private:
        QString m_text;
};

#endif // MYCLASS_H

// --- myclass.cpp ---

#include "myclass.h"

MyClass::MyClass(QObject* parent) : QObject(parent)
{
} // constructor

MyClass::~MyClass()
{
} // destructor

QString MyClass::text() const
{
        return m_text;
} // text

void MyClass::setText(const QString& text)
{
        m_text = text;
} // setText

void MyClass::append(const QString& text)
{
        m_text.append(text);
} // append







S> What I am trying to do is add some helper functions to the global
S> object. However with certain property names the program crashes while
S> deleting the engine. For example when I set the properties 'log',
S> 'print' or 'test' all works. If I however set other properties for
S> example with the names 'lerror' or 'perror', the crash in the engine
S> destructor happens.

S> I have reduced it to the small as possible testcase below. When run as
S> it is, all is ok. When one or both lines of perror or lerror are
S> uncomented, it crashes.

S> I am using Qt commercial 4.3.0 on windows with visual studio 2005.

S> main.c:

S> #include <QCoreApplication>
S> #include <QtDebug>
S> #include <QScriptEngine>
S> #include <QStringList>

S> static QScriptValue f(QScriptContext* c, QScriptEngine* e)
S> {
S>         qDebug() << c->callee().toString();
S>         return QScriptValue();
S> }

S> int main(int argc, char *argv[])
S> {
S>         QCoreApplication a(argc, argv);
S>         QScriptEngine e;
S>         e.globalObject().setProperty("log", e.newFunction(f));
S>         e.globalObject().setProperty("print", e.newFunction(f));
S>         e.globalObject().setProperty("test", e.newFunction(f));
S>         //e.globalObject().setProperty("lerror", e.newFunction(f));
S>         //e.globalObject().setProperty("perror", e.newFunction(f));
S>         QScriptValue r = e.evaluate(
S>                 "print('x');\n"
S>                 "log('x');\n"
S>                 "test('x');\n"
S>         );
S>         if (!r.isUndefined()) qDebug() << r.toString();
S>         if (e.hasUncaughtException()) {
S>                 QStringList b = e.uncaughtExceptionBacktrace();
S>                 qDebug() << b.join("\n");
S>         }
S> }

--
 [ signature omitted ] 

Message 3 in thread

On 18.06.07 16:32:50, Seneca wrote:
> While further experimenting with QtScript I see that this problem is
> not only with functions, but can raise with any property added to the
> global object. Also this second sample will cause a crash in the heap
> deallocation at program end. I tried qt 4.3.0 binary commercial
> distribution as well as 4.3.1 snapshot 18.6.2007, both with
> Visual Studio 2005 SP1.
> 
> Can anybody confirm this, or point out where the mistake could be at
> my side, please?

I can confirm that both of your examples work fine here, but I don't
have the slightest idea why they crash for you or wether you're doing
something wrong.

Andreas

-- 
 [ signature omitted ] 

Message 4 in thread

Maybe I need to add, that I only get the bug when running in the
debugger. Everything seems ok when running without debugger.

The message I get is:

 "Windows has triggered a breakpoint in stest.exe.
 This may be due to a corruption of the heap, and indicates a bug in
 stest.exe or any of the DLLs it has loaded."

The debugger halts in dbgheap.c at line 2072.



You wrote at Montag, 18. Juni 2007 17:03:

AP> On 18.06.07 16:32:50, Seneca wrote:
>> While further experimenting with QtScript I see that this problem is
>> not only with functions, but can raise with any property added to the
>> global object. Also this second sample will cause a crash in the heap
>> deallocation at program end. I tried qt 4.3.0 binary commercial
>> distribution as well as 4.3.1 snapshot 18.6.2007, both with
>> Visual Studio 2005 SP1.
>> 
>> Can anybody confirm this, or point out where the mistake could be at
>> my side, please?

AP> I can confirm that both of your examples work fine here, but I don't
AP> have the slightest idea why they crash for you or wether you're doing
AP> something wrong.

AP> Andreas

AP> -- 
AP> Are you a turtle?

AP> --
AP> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
AP> List archive and information: http://lists.trolltech.com/qt-interest/




-- 
 [ signature omitted ] 

Message 5 in thread

On 18.06.07 17:17:04, Seneca wrote:
> Maybe I need to add, that I only get the bug when running in the
> debugger. Everything seems ok when running without debugger.
> 
> The message I get is:
> 
>  "Windows has triggered a breakpoint in stest.exe.
>  This may be due to a corruption of the heap, and indicates a bug in
>  stest.exe or any of the DLLs it has loaded."
> 
> The debugger halts in dbgheap.c at line 2072.

Running it through a debugger (I'm on Linux so this is gdb) doesn't show
anything. Maybe you're debugger is a bit buggy ;)

Andreas

-- 
 [ signature omitted ] 

Message 6 in thread

AP> Running it through a debugger (I'm on Linux so this is gdb)
AP> doesn't show anything. Maybe you're debugger is a bit buggy ;)

Thanks for your investigation. Basicly I wouldn't exclude that,
especially since VS2005 is in fact even with SP1 still quite unstable.
However I never have seen such a problem with heap cleanup in Qt
projects until I started using QtScript. Either the problem is on
windows only, or gdb does not do such deep heap checking as the VS2005
debugging environment.

If somebody else with similar development environment could confirm
the problem, I guess it would justify to file a bug report in the
trolltech task tracker.

My testing environments:

  - Pentium 4 tower with 1 GB, Windows XP SP2 and all curent patches
  - Core duo laptop with 2 GB, Windows as above
  - Visual studio 2005 professional, no patches or SP installed
  - Visual studio 2005 professional, with SP1
  - qt-vsintegration-1.3.0
  - qt-win-commercial-4.3.0-vs2005
  - qt-win-commercial-src-4.3.1-snapshot-20070618

--
 [ signature omitted ] 

Message 7 in thread

Hello!

Seneca schrieb:
> Maybe I need to add, that I only get the bug when running in the
> debugger. Everything seems ok when running without debugger.
> 
> The message I get is:
> 
>  "Windows has triggered a breakpoint in stest.exe.
>  This may be due to a corruption of the heap, and indicates a bug in
>  stest.exe or any of the DLLs it has loaded."
> 
This looks somewhat familar: are you mixing release and debug libraries?
For example: are you using a release-compiled Qt with a debug-compiled 
executable?

Regards,
     Jan

--
 [ signature omitted ] 

Message 8 in thread

KJ> This looks somewhat familar: are you mixing release and debug libraries?
KJ> For example: are you using a release-compiled Qt with a debug-compiled 
KJ> executable?

That was it!!!

I had linked QtScript4.lib instead of QtScriptd4.lib. After changing
to the debug version of the library, everything works fine now.

Thank you very much!

--
 [ signature omitted ]