Segmentation Fault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Saad Bin Ahmed
    New Member
    • Jul 2011
    • 25

    Segmentation Fault

    Hello,

    I got segmentation fault error due to problem in following code. If somebody knows how can I write it another way then please share it.

    const DOMElement* netElement = (DOMElement*) rootNode->getElementsByT agName(XMLStrin g::transcode("N et"))->item(0);

    When I removed ->item(0) at the the end of above mentioned code then segmentation error removed but program Aborted during execution.

    So, in both above mentioned situations please tell me how can I handle this type of situation?

    Thanks in advance.
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Perhaps there are no elements with the name "Net", getElementsByta gname would return NULL and you would be dereferencing a NULL pointer.

    Do not chain your method calls like this unless you can be absolutely certain that they are going to return a valid result; which generally speaking you never can be.

    Comment

    • Saad Bin Ahmed
      New Member
      • Jul 2011
      • 25

      #3
      I am giving xml file as an input to this program. The statement shows above is use to read xml Tags, Net is a Tag name and it contains value.

      Comment

      • Banfa
        Recognized Expert Expert
        • Feb 2006
        • 9067

        #4
        Is the tag name really "Net" or is it "net" or some other case difference. XML is case sensitive.

        Regardless getElementsByTa gName returning NULL is the most likely cause and as I said would should not chain calls you are not 100% sure will work.

        Instead try

        Code:
        DOMNodeList *nodeList = rootNode->getElementsByTagName(XMLString::transcode("Net");
        
        if (nodeList != NULL)
        {
            const DOMElement* netElement = (DOMElement*) nodeList->item(0);
        }
        else
        {
            // Output an error message
        }
        Then you can start properly diagnosing your problem.

        Comment

        Working...