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



  • 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



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



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


  • Qt Champions 2016

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



  • This post is deleted!

Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.