[Solved] QDomDocument problem
-
Hi! I have problem with XML parsing... It's a rather strange problem. Ill just drop code to see:
@
void Neuro::Initialize()
{
this->_LoadConfiguration();
this->_FindPlugins();
}void Neuro::_LoadConfiguration() { QDomDocument doc("configuration"); QFile file("configuration.xml"); if (!file.open(QIODevice::ReadOnly)) return; if (!doc.setContent(&file)) { file.close(); return; } file.close(); this->Configuration = QSharedPointer<QDomDocument>(&doc); //<<<<< ALL OK QDomElement root = this->Configuration->documentElement(); //<<<<< ALL OK QDomNodeList lista = root.elementsByTagName("plugin_path"); //<<<<< ALL OK QDomNode nod = lista.at(0); //<<<<< ALL OK QString pd = nod.nodeValue(); //<<<<< ALL OK-- } void Neuro::_FindPlugins() { QDomElement root = this->Configuration->documentElement(); //<<< ERROR QDomNodeList lista = root.elementsByTagName("plugin_path"); QDomNode nod = lista.at(0); QString pd = nod.nodeValue(); ........ ........
@
Problem is when i call Initialize function in _LoadConfiguration everything is ok, but in _FindPlugins() same code from _LoadConfiguration() fails with error SegmentationFault. What am i trying to achive is to store xml config as DOM model in memory, make adjustments during program execution and on closing to save it to file.
Code like this breaks at first line and if i change first & second line to
@QDomNodeList lista = this->Configuration->elementsByTagName("plugin_path");@
result is 0 element list. This is the xml file:
@
<configuration>
<app>
<plugin_path>./plugins</plugin_path>
</app>
</configuration>
@I checked memory addresses of pointer inside QSharedPointer Configuration and they are same in both function calls.
Some info? Just to mention I am developing with latest SDK 4.7 on Windows 7 SP1 using Qt Creator
-
I have found a solution but don't know why is that helping.
Solution is to use
@QDomDocument Conf;@ instead of @QSharedPointer<QDomDocument> Configuration;@ at member declaration. I dont get it why i need to use stack instead of heap memory. I think that QSharedPointer messes up something but couldn find how... I have tried this in _LoadConfiguration() and it works but still gives error in _FindPlugins().
@
{ QDomElement root = this->Configuration->documentElement(); QDomNodeList lista = root.elementsByTagName("plugin_path"); QDomNode nod = lista.at(0); QString pd = nod.nodeValue(); } QDomElement root2 = this->Configuration->documentElement(); QDomNodeList lista2 = root2.elementsByTagName("plugin_path"); QDomNode nod2 = lista2.at(0); QString pd2 = nod2.nodeValue();
@
-
The problem is with
QDomDocument doc;
...
this->Configuration = QSharedPointer<QDomDocument>(&doc);You take the address from an object on stack, which is destroyed when the block (here when leaving the method).
So after leaving _LoadConfiguration, the Configuration member holds an invalid pointer. Access it -> Crash.
It would also crash later when the shared pointer is destroyed. Double deletion -> Crash.
SharedPointers only make sense if nobody other than the sharedptr (and its copies and weakptrs) messes with the object's lifetime. A sharedptr assumes ownership and thus will delete the object in the end. It can't detect when someone else already did so.You can either create it as pointer right away and put it into a shared pointer:
QSharedPointer<QDomDocument>( new QDomDocument )
or even better, don't use any pointer at all, as you suggest yourself in your update.
-
Thanks! Its working.
[quote author="chetankjain" date="1288933889"]Problem is with this line:
@
QDomDocument doc("configuration");
@use a pointer there ...
[/quote]