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

Usman Ajmal uzmanajmal at gmail.com
Sat Jul 25 19:54:24 CEST 2009


yeah you both are right. I thought i created person on heap but i overlooked
it. Thanks
On Sat, Jul 25, 2009 at 10:39 PM, <qt-interest at status.e4ward.com> wrote:

> Yeah, from the code it looks like you need to allocate a new person_info
> for *person to point to.
>
>
> On Jul 25, 2009, at 1:08 AM, Usman Ajmal wrote:
>
> Hi,
>
> I am trying to parse an xml which contain, say
>
> <human>
> <male>
>       <name>usman</name>
>       <age>21</age>
> </male>
> <male>
>       <name>fedrick</name>
>       <age>23</age>
> </male>
> </human>
>
> 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?
>
> ---
> Usman
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
>
> _______________________________________________
> Qt-interest mailing list
> Qt-interest at trolltech.com
> http://lists.trolltech.com/mailman/listinfo/qt-interest
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20090725/cea0e323/attachment.html 


More information about the Qt-interest-old mailing list