Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QScriptEngine memory leak ??
Forum Updated to NodeBB v4.3 + New Features

QScriptEngine memory leak ??

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 565 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Martin.Wells
    wrote on last edited by
    #1

    Guys,please help me!
    Why QScriptEngine doesn't release memory
    微信截图_20240314114346.png
    b0a15c53-b472-4491-b46b-005864fd8603-image.png
    Why var a is not deleted? I think var a should be rdeleted after the function call.
    What should I do?
    Please help me!

    C 1 Reply Last reply
    0
    • M Martin.Wells

      Guys,please help me!
      Why QScriptEngine doesn't release memory
      微信截图_20240314114346.png
      b0a15c53-b472-4491-b46b-005864fd8603-image.png
      Why var a is not deleted? I think var a should be rdeleted after the function call.
      What should I do?
      Please help me!

      C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #2

      @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 use let.

      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.

      M 1 Reply Last reply
      3
      • C ChrisW67

        @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 use let.

        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.

        M Offline
        M Offline
        Martin.Wells
        wrote on last edited by
        #3

        @ChrisW67 said in QScriptEngine memory leak ??:

        Different result if you use let.

        sorry ,Does qt5.15.2 QScriptEngine support let?
        ba31e5f3-f21a-4096-bfa5-d635e234e36d-image.png
        return error "SyntaxError: Parse error"

        Could you write a simple example? thankyou!

        jsulmJ C 2 Replies Last reply
        0
        • M Martin.Wells

          @ChrisW67 said in QScriptEngine memory leak ??:

          Different result if you use let.

          sorry ,Does qt5.15.2 QScriptEngine support let?
          ba31e5f3-f21a-4096-bfa5-d635e234e36d-image.png
          return error "SyntaxError: Parse error"

          Could you write a simple example? thankyou!

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @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?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          M 1 Reply Last reply
          0
          • jsulmJ jsulm

            @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?

            M Offline
            M Offline
            Martin.Wells
            wrote on last edited by
            #5

            @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"

            1 Reply Last reply
            0
            • M Martin.Wells

              @ChrisW67 said in QScriptEngine memory leak ??:

              Different result if you use let.

              sorry ,Does qt5.15.2 QScriptEngine support let?
              ba31e5f3-f21a-4096-bfa5-d635e234e36d-image.png
              return error "SyntaxError: Parse error"

              Could you write a simple example? thankyou!

              C Offline
              C Offline
              ChrisW67
              wrote on last edited by ChrisW67
              #6

              @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.

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved