PDA

View Full Version : unhandled exception in release mode when calling xmlRoot->firstChild()



richardander
21st January 2009, 20:31
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!




QFile aFile( strFilename );
if ( !aFile.exists() )
return;

QDomDocument document;

bool bSuccess = document.setContent( &aFile, &errorString, &errorLine, &errorColumn );
if ( !bSuccess )
return;

QDomElement * xmlRoot = &(document.documentElement());
if( xmlRoot->isNull())
return;

// Read the first child
QDomNode tempDomNode = xmlRoot->firstChild();

...

jpn
24th January 2009, 17:40
You're storing a pointer to a temporary object returned by QDomDocument::documentElement().

QDomElement * xmlRoot = &(document.documentElement());
should be


QDomElement xmlRoot = document.documentElement();

richardander
27th January 2009, 00:55
It works! Thank you!

But, I don't know

- why I can't use pointer?

- why the pointer works in debug mode?

thank you!

jpn
27th January 2009, 10:31
- 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.