Results 1 to 4 of 4

Thread: unhandled exception in release mode when calling xmlRoot->firstChild()

  1. #1
    Join Date
    Dec 2008
    Posts
    68

    Default unhandled exception in release mode when calling xmlRoot->firstChild()

    Hello everyone,

    I am trying to access the first child node in an XML file with the following code. it works when I did it in DEBUG mode. However, when I run it in RELEASE mode, I always got error at this line of code:
    QDomNode tempDomNode = xmlRoot->firstChild();

    the error message is: Unhandled exception at 0x670015b5 in aTest.exe: 0xC0000005: Access violation writing location 0x00530058.

    when I click the break button in Visual Studio, it shows the free.c file.

    Can anyone let me know the problem or the possible reason?

    thank you!

    Qt Code:
    1. QFile aFile( strFilename );
    2. if ( !aFile.exists() )
    3. return;
    4.  
    5. QDomDocument document;
    6.  
    7. bool bSuccess = document.setContent( &aFile, &errorString, &errorLine, &errorColumn );
    8. if ( !bSuccess )
    9. return;
    10.  
    11. QDomElement * xmlRoot = &(document.documentElement());
    12. if( xmlRoot->isNull())
    13. return;
    14.  
    15. // Read the first child
    16. QDomNode tempDomNode = xmlRoot->firstChild();
    17.  
    18. ...
    To copy to clipboard, switch view to plain text mode 
    Last edited by jpn; 24th January 2009 at 16:39. Reason: changed [quote] to [code] tags

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unhandled exception in release mode when calling xmlRoot->firstChild()

    You're storing a pointer to a temporary object returned by QDomDocument::documentElement().
    Qt Code:
    1. QDomElement * xmlRoot = &(document.documentElement());
    To copy to clipboard, switch view to plain text mode 
    should be
    Qt Code:
    1. QDomElement xmlRoot = document.documentElement();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  3. #3
    Join Date
    Dec 2008
    Posts
    68

    Default Re: unhandled exception in release mode when calling xmlRoot->firstChild()

    It works! Thank you!

    But, I don't know

    - why I can't use pointer?

    - why the pointer works in debug mode?

    thank you!

  4. #4
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: unhandled exception in release mode when calling xmlRoot->firstChild()

    Quote Originally Posted by richardander View Post
    - why I can't use pointer?
    Because QDomDocument::documentElement() returns by value, not by reference. The temporary copy goes out of scope immediately after the statement, according to normal C++ rules. Thus, you must not store a pointer to something that doesn't exist after the statement.

    - why the pointer works in debug mode?
    That's pure luck, I'd say. The code is still incorrect.
    J-P Nurmi

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.