Results 1 to 7 of 7

Thread: QDomElement::text() returns corrupted strings

  1. #1
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default QDomElement::text() returns corrupted strings

    Hi,

    please explain what is wrong with my code:

    Qt Code:
    1. #include <QDomDocument>
    2. #include <QFile>
    3.  
    4. int main(int argc, char *argv[])
    5. {
    6.  
    7. QFile file("c:\\bugtest.xml");
    8. file.open(QIODevice::ReadOnly);
    9. doc.setContent(&file);
    10. file.close();
    11.  
    12. QDomElement docElem = doc.documentElement();
    13.  
    14. QDomNode n = docElem.firstChild();
    15. while(!n.isNull()) {
    16. QDomElement e = n.toElement(); // try to convert the node to an element.
    17. if(!e.isNull()) {
    18. if (e.tagName() == "value" && e.hasAttribute("id"))
    19. {
    20. QString id = e.attribute("id");
    21. QString data = e.text();
    22. } /* crash point */
    23. n = n.nextSibling();
    24. }
    25. }
    26. }
    To copy to clipboard, switch view to plain text mode 
    The code crashes at the point marked, when deleting QString data - heap corrupted.

    The document is:

    xml Code:
    1. <?xml version="1.0" encoding="utf-8"?>
    2. <data>
    3. <value id="some_id">Some value</value>
    4. </data>
    To copy to clipboard, switch view to plain text mode 

    Windows XP, Visual studio 2005, QT 4.5.0-rc1 (also 4.3.5).


    Thanks in advance.
    Last edited by wysota; 7th March 2009 at 13:20. Reason: missing [code] tags

  2. #2
    Join Date
    Apr 2008
    Posts
    104
    Thanks
    8
    Thanked 7 Times in 7 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QDomElement::text() returns corrupted strings

    As far I see, you need to implement a recursive XML-tree walker, because yours one cannot reach the "value" element.

    Qt Code:
    1. void step (QDomNode node)
    2. {
    3. for (QDomNode n = node.firstChild(); ! n.isNull(); n = n.nextSibling())
    4. {
    5. QDomElement e = n.toElement();
    6.  
    7. if (e.isNull())
    8. continue;
    9.  
    10. if (e.nodeName().toLower() == "value")
    11. {
    12. QString id = e.attribute ("id");
    13.  
    14. if (! id.isEmpty())
    15. {
    16. QString data = e.text(); //good
    17. }
    18.  
    19. }
    20.  
    21. if (e.hasChildNodes())
    22. step (n);
    23. }
    24. }
    25.  
    26. //and use it:
    27.  
    28. step (doc.documentElement());
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to roxton for this useful post:

    sladecek (7th March 2009)

  4. #3
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDomElement::text() returns corrupted strings

    Thank you for your answer.

    Unfortnatelly, Your code fails in the same way as mine.

    HEAP[visu000.exe]: Invalid Address specified to RtlValidateHeap( 003D0000, 00BE7260 )
    Windows has triggered a breakpoint in visu000.exe.

    This may be due to a corruption of the heap, and indicates a bug in visu000.exe or any of the DLLs it has loaded.

    The output window may have more diagnostic information
    The program '[1532] visu000.exe: Native' has exited with code 0 (0x0).


    My code can read the value . The string data contains "Some value" as expected. But when the QString "data" is about to be deleted and the destructor is called the program crashes due to heap corruption.

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomElement::text() returns corrupted strings

    Is this the exact code that crashes? It seems to work fine for me. If it's part of some larger application, please provide some background information (like if you use threads or not) or a compilable example reproducing the problem.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  6. #5
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDomElement::text() returns corrupted strings

    Thank you for testing my code.

    Yes, exactly this code crashes. Moreover, I tried the SAX API in meantime and it crashes too:

    Qt Code:
    1. class Parser: public QXmlDefaultHandler;
    2.  
    3. bool Parser::characters ( const QString & ch )
    4. {
    5. m_cData.append(ch); // crash
    6. return true;
    7. }
    To copy to clipboard, switch view to plain text mode 

    I use the free version of QT compiled from source by MS VC++. Maybe the Qt XML library was compiled badly?

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QDomElement::text() returns corrupted strings

    Maybe. Do examples bundled with Qt work? Also, when it crashes try debugging to see where the crash occurs.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #7
    Join Date
    Mar 2009
    Posts
    4
    Thanks
    1
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QDomElement::text() returns corrupted strings

    Thank you for you effort.

    Problem solved. Wrong library name in library inputs. I linked with QtXml4.lib instead of correct QtXmlD4.lib.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.