QScriptEngine memory leak ??
-
Guys,please help me!
Why QScriptEngine doesn't release memory
Why var a is not deleted? I think var a should be rdeleted after the function call.
What should I do?
Please help me! -
@Martin-Wells said in QScriptEngine memory leak ??:
Why var a is not deleted? I think var a should be rdeleted after the function call.
Why do you think the persistent JS variable you declared with
var
in global scope should be deleted?// At time 0 mEngine.evaluate("var a = 42"); // At some time later qDebug() << "the magic number is:" << mEngine.evaluate("a").toNumber(); // Result: the magic number is: 42
If
a
is set to 2 GB of data then that's what stays used for the life of the QScriptEngine.
Different result if you uselet
.What should I do?
Stop using Windows task manager as any indicator of real-time heap usage by your program. Even if that 2GB block was freed by your code Windows may not reclaim that RAM until it is needed elsewhere.
-
@ChrisW67 said in QScriptEngine memory leak ??:
Different result if you use let.
sorry ,Does qt5.15.2 QScriptEngine support let?
return error "SyntaxError: Parse error"Could you write a simple example? thankyou!
-
@Martin-Wells Please post code as text, not pictures.
In your latest code you're using Test.bytearray() instead of Test.string() and you also use {} - why not try exact same code but with let instead of var? -
@jsulm sorry , this my code
//Test object
QString Test::string()
{
QString string=QString(10241024500,'A');
return string;
}// mainwindow.cpp regist Test object
Test*test=new Test(this);
QScriptValue value = engine.newQObject(test);
engine.globalObject().setProperty("Test", value);//call function
QScriptValue func = engine.evaluate(" let a = Test.string() ");
if(engine.hasUncaughtException())
{
qDebug()<<engine.uncaughtException().toString();
}Then I get the same error "SyntaxError: Parse error"
-
@Martin-Wells said in QScriptEngine memory leak ??:
sorry ,Does qt5.15.2 QScriptEngine support let?
Evidently not. Not too surprising given that QtScript is long deprecated.
Setting the value to undefined (or even an empty string) should give the Javascript garbage collector a chance.
Does this scratch your itch?#include <QCoreApplication> #include <QObject> #include <QScriptEngine> #include <QString> #include <QDebug> class Test: public QObject { Q_OBJECT public: Test(QObject *p = nullptr): QObject(p) { } ~Test() { } public slots: QString string() { return QString(10*1024*1024, 'x'); } }; int main(int argc, char *argv[]) { QCoreApplication a(argc, argv); QScriptEngine engine; Test *test = new Test(qApp); QScriptValue objectValue = engine.newQObject(test); engine.globalObject().setProperty("Test", objectValue); // At time 0 engine.evaluate("var a = Test.string()"); if(engine.hasUncaughtException()) { qDebug()<<engine.uncaughtException().toString(); } // At some time later qDebug() << "The string length is:" << engine.evaluate("a.length").toNumber(); // and later still engine.evaluate("a = undefined"); if(engine.hasUncaughtException()) { qDebug()<<engine.uncaughtException().toString(); } qDebug() << "The string length is:" << engine.evaluate("a.length").toNumber(); return a.exec(); } #include "main.moc"
Why do you want a couple of GB in a Javascript environment?
Please use the
</>
tool in the editor when posting code.