0

Good day. The problem is the following. I have a valid *.xml file which I try to parse using the following code:

for(XMLSize_t i = 0; i < childrenNodeCount; ++i)
{
  DOMNode* currentNode = children->item(i);

  if ((currentNode->getNodeType() != 0) && (currentNode->getNodeType() == DOMNode::ELEMENT_NODE))
  {
    DOMElement* currentElement = dynamic_cast<xercesc::DOMElement*>(currentNode); // !!!

    if (XMLString::equals(currentElement->getTagName(), TAG_SectionHeader))
    {
      // parse this part
    }

    if (XMLString::equals(currentElement->getTagName(), TAG_SectionBody))
    {
      // parse this part
    }
  }
}

Program crashes during execution with a SIGILL on first "equals" check. Debugging showed, that after the dynamic cast, currentElement is actually a null pointer. What can be the problem here?

Compiling with xlc++, Xerces library 2.5, AIX 7.

P.S. same code works fine on Windows apparently.

UPDATE: changing dynamic_cast to static_cast made the code run without errors. However it left some questions unanswered.

1) Why did the code run without errors while using dynamic_cast on Windows but not on Unix? Could it be a compiler or a library version issue?

2) Is there a better/cleaner way to cast in this case?

HighPredator
  • 790
  • 4
  • 21
  • `currentNode` is not a `DOMElement`. Oh and `currentNode->getNodeType() != 0)` is totally unnecessary. – Captain Obvlious Aug 20 '15 at 11:29
  • Indeed, it is a DOMNode pointer. That's exactly the point. I'm trying to make a cast to derived class pointer. – HighPredator Aug 20 '15 at 11:58
  • `dynamic_cast` returns null when the conversion cannot performed. Since `currentNode` is _not_ a `DOMElement` the cast fails and null is returned. When `currentNode` is not a `DOMElement` and you use `static_cast` instead the result is undefined behavior. Any good debugger will tell you what type `currentNode` really is. – Captain Obvlious Aug 20 '15 at 16:22
  • @CaptainObvlious, ok. So. The situation is the following: DOMNode is a parent of DOMElement. Both of those classes are pure virtual. The xerces examples I used to construct that code used dynamic casts. Even here that is done http://stackoverflow.com/questions/4746631/passing-from-a-domnode-to-a-domelement-in-xerces-c What should I do? – HighPredator Aug 21 '15 at 07:10

2 Answers2

2

You didn't mention the compiler version or options, have you specified -qrtti to enable dynamic_cast?

    -qrtti=<option> | -qnortti
            (C++) Generates runtime type identification (RTTI)
            information for the typeid and dynamic_cast
            operators.  The suboptions are:

            all
                 Generates the information needed for the RTTI
                 typeid and dynamic_cast operators.
            type | typeinfo
                 Generates the information needed for the RTTI
                 typeid operator only.
            dyna | dynamiccast
                 Generates the information needed for the RTTI
                 dynamic_cast operator only.

            Default: -qnortti
0

Ok, the reason was the following: the Xerces library is not built by default with RTTI enabled. To make mechanics in the OP work, the library should be rebuilt with RTTI enabled. https://issues.apache.org/jira/browse/XERCESC-819

HighPredator
  • 790
  • 4
  • 21