[Qt-interest] Segmentation Fault encountered while assigning text of QDomElement to a QString variable

Stephen Jackson spjackson42 at gmail.com
Sat Jul 25 10:45:37 CEST 2009


On Sat, Jul 25, 2009 at 9:08 AM, Usman Ajmal wrote:
>
> When i run following program i get segmentation fault at the bold line of
> the source code below
>
> //my person_info class
> class person_info {
> public:
> QString name;
> QString age;
> person_info()
> {
>     name = "";
>     age = ""
> }
> };
> person_info *person;
>
>
> //main.cpp
> QDomElement docElem = doc.documentElement();
>   traverseXML(docElem);
>
> //Taverse XML function
> void traverseXML(QDomElement docElem)
> {
>     QDomNode n = docElem.firstChild();
>     while(!n.isNull())
>         {
>         QDomElement e = n.toElement(); // try to convert the node to an
> element
>         if(!e.isNull()) {
>             QString str =  e.tagName();
>             traverseXML(e);
>             cout<<str<<"   ";
>              if (str =="name")
>                     person->name = e.text(); //segmentation fault occurs
> here. If i use 'QString test = e.text()' in its place then no segmentation
> fault occurs here
>              if (str=="age")
>                     person->age = e.age();
>             }
>         }
>              n = n.nextSibling();
>          }
>     if (n.isNull())
>         return;
> }
>
>
> Any idea what is causing this Seg. Fault?
>

At the risk of stating the blindingly obvious... a segmentation fault
on a line that dereferences a pointer suggests that the pointer may
not be valid.

In the fragment of code you have posted, you have a pointer "person"
but this pointer is never set to any valid address.

Perhaps you are setting the pointer properly in code that you haven't
shown, but my crystal ball isn't showing me that.

If that's not the problem, then try using a debugger.

Stephen Jackson




More information about the Qt-interest-old mailing list