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. QJSEngine cleanup/destructor

QJSEngine cleanup/destructor

Scheduled Pinned Locked Moved Solved General and Desktop
qjsenginejavascriptqt 5.12destructorcleanup
14 Posts 3 Posters 3.0k 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.
  • SGaistS Offline
    SGaistS Offline
    SGaist
    Lifetime Qt Champion
    wrote on last edited by
    #5

    Did you already check the bug report system for something related ?

    Interested in AI ? www.idiap.ch
    Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

    Y 1 Reply Last reply
    0
    • SGaistS SGaist

      Did you already check the bug report system for something related ?

      Y Offline
      Y Offline
      yah_nosh
      wrote on last edited by
      #6

      @SGaist Yeah, I looked around, nobody seems to have the same problem. Should I report it there?

      Also, just to clarify: I should be able to create and destroy QJSEngine objects this way? So it's not like I'm trying to make the framework do something it's not supposed to?

      1 Reply Last reply
      0
      • SGaistS Offline
        SGaistS Offline
        SGaist
        Lifetime Qt Champion
        wrote on last edited by
        #7

        Although I haven't used that class extensively, I am not aware of such a limitation. So go on and create a report. Please provide a minimal compilable example.

        Interested in AI ? www.idiap.ch
        Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

        Y 1 Reply Last reply
        2
        • SGaistS SGaist

          Although I haven't used that class extensively, I am not aware of such a limitation. So go on and create a report. Please provide a minimal compilable example.

          Y Offline
          Y Offline
          yah_nosh
          wrote on last edited by
          #8

          @SGaist While making the minimal compilable example, I managed to find the actual source of the problem. It wasn't the destructor at all, at least not directly, but instead me exposing my "interface" to JavaScript. Below is a small example:

          QJSEngine* jsEngine = new QJSEngine();
          
          // Add ourselves to the JS engine
          QJSValue interface = jsEngine->newQObject(interfacePtr);
          
          // Expose interface in the global namespace
          jsEngine->globalObject().setProperty("INTERFACE", interface);
          
          // Do stuff...
          
          delete jsEngine; // <-- error happens here
          
          

          It seems that using newQObject causes trouble once I try destroying the QJSEngine, and the documentation references this as well:

          If the given object is deleted outside of the engine's control, any attempt to access the deleted QObject's members through the JavaScript wrapper object (either by script code or C++) will result in a script exception.

          So I suspect I need to "clean up" this object before deleting the engine, otherwise (presumably) some ownership issues come up when the JS heap is cleaned up. Unfortunately, I have no idea how to fix this. I tried using deleteProperty on the global object, but that did not work.

          JonBJ 1 Reply Last reply
          1
          • Y yah_nosh

            @SGaist While making the minimal compilable example, I managed to find the actual source of the problem. It wasn't the destructor at all, at least not directly, but instead me exposing my "interface" to JavaScript. Below is a small example:

            QJSEngine* jsEngine = new QJSEngine();
            
            // Add ourselves to the JS engine
            QJSValue interface = jsEngine->newQObject(interfacePtr);
            
            // Expose interface in the global namespace
            jsEngine->globalObject().setProperty("INTERFACE", interface);
            
            // Do stuff...
            
            delete jsEngine; // <-- error happens here
            
            

            It seems that using newQObject causes trouble once I try destroying the QJSEngine, and the documentation references this as well:

            If the given object is deleted outside of the engine's control, any attempt to access the deleted QObject's members through the JavaScript wrapper object (either by script code or C++) will result in a script exception.

            So I suspect I need to "clean up" this object before deleting the engine, otherwise (presumably) some ownership issues come up when the JS heap is cleaned up. Unfortunately, I have no idea how to fix this. I tried using deleteProperty on the global object, but that did not work.

            JonBJ Offline
            JonBJ Offline
            JonB
            wrote on last edited by
            #9

            @yah_nosh
            Does https://stackoverflow.com/questions/26177693/qjsengine-deletes-my-qobject-how-to-change-ownership-after-qjsenginenewqobjec (QQmlEngine::setObjectOwnership()) help your situation at all?

            Y 1 Reply Last reply
            3
            • Y Offline
              Y Offline
              yah_nosh
              wrote on last edited by
              #10

              Okay, found the solution. I needed to change the ownership using QQmlEngine::setObjectOwnership, and the JS engine stops trying to delete my objects. So technically there was no bug at all, but the source code and documentation sorely needs an update.

              1 Reply Last reply
              3
              • JonBJ JonB

                @yah_nosh
                Does https://stackoverflow.com/questions/26177693/qjsengine-deletes-my-qobject-how-to-change-ownership-after-qjsenginenewqobjec (QQmlEngine::setObjectOwnership()) help your situation at all?

                Y Offline
                Y Offline
                yah_nosh
                wrote on last edited by
                #11

                @JonB Yeah, sorry, posted my comment right before I noticed yours. It's really unintuitive that the solution is buried in a derived class static function... which apparently uses the base class code anyway.

                1 Reply Last reply
                0
                • SGaistS Offline
                  SGaistS Offline
                  SGaist
                  Lifetime Qt Champion
                  wrote on last edited by
                  #12

                  @yah_nosh said in QJSEngine cleanup/destructor:

                  Okay, found the solution. I needed to change the ownership using QQmlEngine::setObjectOwnership, and the JS engine stops trying to delete my objects. So technically there was no bug at all, but the source code and documentation sorely needs an update.

                  You should consider improving the documentation and submit a patch for that so everybody can benefit from the outcome of your findings.

                  Interested in AI ? www.idiap.ch
                  Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                  Y 1 Reply Last reply
                  2
                  • SGaistS SGaist

                    @yah_nosh said in QJSEngine cleanup/destructor:

                    Okay, found the solution. I needed to change the ownership using QQmlEngine::setObjectOwnership, and the JS engine stops trying to delete my objects. So technically there was no bug at all, but the source code and documentation sorely needs an update.

                    You should consider improving the documentation and submit a patch for that so everybody can benefit from the outcome of your findings.

                    Y Offline
                    Y Offline
                    yah_nosh
                    wrote on last edited by
                    #13

                    @SGaist Yeah, I think as soon as this project is done, I'll append the relevant parts in the QJSEngine docs so others can avoid this headache. Can anyone contribute to the documentation?

                    1 Reply Last reply
                    1
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by
                      #14

                      Anyone can contribute to the documentation, the code, etc. You have to follow the procedures and rules but it's not some walled garden.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      2

                      • Login

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