Error while saving XML file in Windows



  • Hi,

    I am writing a Qt application that saves its data as an XML file. The saving process is really simple, a QPushButton triggers the save() slot described below:

    void save()
    {
        QFile documentFile(projectDir.path() + "/document.xml");
        if (documentFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
            QTextStream ts(&documentFile);
            QDomDocument doc;
            doc.appendChild(project->saveAsXml(doc));
            ts << doc.toString();
            documentFile.close();
        } 
    }  
    

    The method "saveAsXml()" returns a QDomElement object with the whole XML structure of the document. I have been testing this code in Unix/Mac systems without any problem, I mean, the document is always saved successfully.
    Nevertheless, in Windows systems I have been detecting a terrible issue: the XML file is getting corrupted after the saving process. The document files got a huge size (several MB) because the inner structure of the XML file is repeated and repeated hundreds of times with no apparent reason.

    I'm pretty sure that this issue only occurs on Windows systems (using exactly the same source code that I use in Unix). So, my question is: what should I do to fix the problem in Windows? I appreciate any advice about it.



  • Hi @xtingray, that does sound nasty.

    This line looks suspiciously recursive to me, and may be subject to subtle differences in compiler optimisations:

    doc.appendChild(project->saveAsXml(doc));
    

    Can you try re-writing that as two separate statements? (just to see if its the cause of the issue).

    Cheers.



  • @Paul-Colby Thank you for your comment. I was checking the implementation of the method "saveAsXml()" and effectively, it is recursive.
    I could try to change the way it works right now, but it will take me some time because it is huge. Maybe I will do it in the middle term.

    Anyway, I can't help to keep asking: why this code works so well in Unix, but so bad in Windows if it is the same implementation? I guess the memory/process management is different between platforms and maybe this can cause some issues, but not sure at all.


  • Qt Champions 2017

    @xtingray said:

    I could try to change the way it works right now, but it will take me some time because it is huge. Maybe I will do it in the middle term.

    I think you should regardless. QDomDocument keeps the whole structure in memory and the overhead is significant.

    I guess the memory/process management is different between platforms and maybe this can cause some issues, but not sure at all.

    It is different, and Linux makes many (but not all) things better, that is topped by the fact that the MS compiler is not known for its good code generation. However, such conclusions are purely speculative and ultimately superficial, so I think it's not productive even guessing about it. My suggestion would be to directly write down the XML structure through a QTextStream instance, or even better to use the QXmlStreamWriter class.

    Kind regards.



  • @kshegunov Thank you for the advice. I am looking for saving memory consumption in my implementation, so I will follow your recommendations :)


Log in to reply
 

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