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. What happens with QDomElement after its QDomDocument goes out of scope?

What happens with QDomElement after its QDomDocument goes out of scope?

Scheduled Pinned Locked Moved Unsolved General and Desktop
domqdomdocumentqdomelement
5 Posts 3 Posters 2.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.
  • B Offline
    B Offline
    Bart_Vandewoestyne
    wrote on 31 May 2016, 20:49 last edited by
    #1

    I am trying to understand whether the following code is fine or not:

    #include <QApplication>
    #include <QDebug>
    #include <QDomDocument>
    #include <QDomElement>
    #include <QString>
    #include <QTextStream>
    
    void printIt(const QDomElement& element)
    {
        QString s;
        QTextStream str(&s);
        element.save(str, 4);
    
        qDebug() << s;
    }
    
    void fillIt(QDomElement& element)
    {
        QString content =
                "  <book>"
                "    <Title>MyBookTitle</Title>"
                "  </book>";
    
        QDomDocument doc;
        doc.setContent(content);
    
        element = doc.documentElement().firstChildElement();
    
        printIt(element);
    
    }  // doc goes out of scope here...
    
    int main(int argc, char* argv[])
    {
        QApplication a(argc, argv);
    
        {
            QDomElement element;
    
            fillIt(element);
    
            printIt(element);  // doc no longer exists, but is element still valid here
                               // because the DOM tree still exists???
    
        }  // Both doc and element are deleted here, so DOM tree also gets deleted???
    
        return 0;
    }
    

    My main question is whether element is still valid after fillIt returns.

    At first, I thought that element could potentially contain rubbish, since its QDomDocument doc already went out of scope and got destroyed, but in the documentation at http://doc.qt.io/qt-4.8/qdomdocument.html i read:

    The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the QDomDocument itself are deleted.

    which makes me believe that element is still valid and the DOM tree still exists. My educated guess is thus that the code is well-defined and the DOM tree will only get deleted right before the return of main, when element goes out of scope.

    Am I right with my assumption? If not, an example showing where things can go wrong would be nice to have...

    For the ones interested: the code example I'm testing with is online at https://github.com/BartVandewoestyne/Qt/tree/master/tests/qdomelement_test

    T 1 Reply Last reply 1 Jun 2016, 05:50
    0
    • B Bart_Vandewoestyne
      31 May 2016, 20:49

      I am trying to understand whether the following code is fine or not:

      #include <QApplication>
      #include <QDebug>
      #include <QDomDocument>
      #include <QDomElement>
      #include <QString>
      #include <QTextStream>
      
      void printIt(const QDomElement& element)
      {
          QString s;
          QTextStream str(&s);
          element.save(str, 4);
      
          qDebug() << s;
      }
      
      void fillIt(QDomElement& element)
      {
          QString content =
                  "  <book>"
                  "    <Title>MyBookTitle</Title>"
                  "  </book>";
      
          QDomDocument doc;
          doc.setContent(content);
      
          element = doc.documentElement().firstChildElement();
      
          printIt(element);
      
      }  // doc goes out of scope here...
      
      int main(int argc, char* argv[])
      {
          QApplication a(argc, argv);
      
          {
              QDomElement element;
      
              fillIt(element);
      
              printIt(element);  // doc no longer exists, but is element still valid here
                                 // because the DOM tree still exists???
      
          }  // Both doc and element are deleted here, so DOM tree also gets deleted???
      
          return 0;
      }
      

      My main question is whether element is still valid after fillIt returns.

      At first, I thought that element could potentially contain rubbish, since its QDomDocument doc already went out of scope and got destroyed, but in the documentation at http://doc.qt.io/qt-4.8/qdomdocument.html i read:

      The parsed XML is represented internally by a tree of objects that can be accessed using the various QDom classes. All QDom classes only reference objects in the internal tree. The internal objects in the DOM tree will get deleted once the last QDom object referencing them and the QDomDocument itself are deleted.

      which makes me believe that element is still valid and the DOM tree still exists. My educated guess is thus that the code is well-defined and the DOM tree will only get deleted right before the return of main, when element goes out of scope.

      Am I right with my assumption? If not, an example showing where things can go wrong would be nice to have...

      For the ones interested: the code example I'm testing with is online at https://github.com/BartVandewoestyne/Qt/tree/master/tests/qdomelement_test

      T Offline
      T Offline
      the_
      wrote on 1 Jun 2016, 05:50 last edited by
      #2

      @Bart_Vandewoestyne

      You declare your element variable in the main function and you pass a reference to this variable to your method. So element is still valid after your fillIt method returns.

      -- No support in PM --

      B 1 Reply Last reply 1 Jun 2016, 06:50
      0
      • T the_
        1 Jun 2016, 05:50

        @Bart_Vandewoestyne

        You declare your element variable in the main function and you pass a reference to this variable to your method. So element is still valid after your fillIt method returns.

        B Offline
        B Offline
        Bart_Vandewoestyne
        wrote on 1 Jun 2016, 06:50 last edited by
        #3

        @the_ said:

        @Bart_Vandewoestyne

        You declare your element variable in the main function and you pass a reference to this variable to your method. So element is still valid after your fillIt method returns.

        I might not have expressed myself well enough. When I wrote 'valid', I actually meant 'having the correct XML element content'. One could reason as follows (I'm not sure if this is the correct reasoning): since doc went out of scope, the QDomDocument where element originally belonged to, no longer exists. That makes me wonder whether element now contains/points_to rubbish/stuff_that_got_deleted or not. My test-program shows that it doesn't, but I am wondering if this is just a lucky shot or rather correct and intended behavior.

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 1 Jun 2016, 07:13 last edited by
          #4

          @Bart_Vandewoestyne said:

          QDomElement

          HI
          Since it stores it text as QStrings its pretty self contained so even
          doc is deleted the text should still be valid.

          One should imagine
          element = doc.documentElement() should point to invalid
          node after, but Im wondering if a copy is made.

          One test could be fun though
          http://doc.qt.io/qt-5/qdomnode.html#ownerDocument
          Should return a null node. (isNull returns true)

          B 1 Reply Last reply 1 Jun 2016, 10:16
          1
          • M mrjj
            1 Jun 2016, 07:13

            @Bart_Vandewoestyne said:

            QDomElement

            HI
            Since it stores it text as QStrings its pretty self contained so even
            doc is deleted the text should still be valid.

            One should imagine
            element = doc.documentElement() should point to invalid
            node after, but Im wondering if a copy is made.

            One test could be fun though
            http://doc.qt.io/qt-5/qdomnode.html#ownerDocument
            Should return a null node. (isNull returns true)

            B Offline
            B Offline
            Bart_Vandewoestyne
            wrote on 1 Jun 2016, 10:16 last edited by Bart_Vandewoestyne 6 Jan 2016, 10:17
            #5
            This post is deleted!
            1 Reply Last reply
            0

            5/5

            1 Jun 2016, 10:16

            • Login

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