Search code examples
c++linuxsegmentation-faulttinyxml

C++ Segmentation Fault - tinyXML


I am encountering a segmentation fault when I am trying to execute my code, it compiles without an errors/warnings but gives the error Segmentation fault when executed. Here is the code snippet below.

XMLElement *pStartTimeStamp = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("RealTimeInformation");
XMLElement *pRealtimeStore = nullptr;
XMLElement *pIter0 = pStartTimeStamp->FirstChildElement("RunID");
while (pIter0 != nullptr) {
    pRealtimeStore = pIter0;
    pIter0 = pIter0->NextSiblingElement("startTimeStamp");
}

if (pRealtimeStore != nullptr) {
    XMLElement *pNewStartTimeStamp = doc.NewElement("startTimeStamp");
    pNewStartTimeStamp->SetText("pNewTimeStamp");
    pStartTimeStamp->InsertAfterChild(pRealtimeStore, pNewStartTimeStamp);

}

EDIT: I tried running gdb but it resulted in

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402ada in tinyxml2::XMLNode::FirstChildElement (this=0x0, value=0x408dd8 "RunID") at tinyxml2.cpp:745
745     for( XMLNode* node=_firstChild; node; node=node->_next ) {

When stepping through the code it fails at this line:

20      XMLElement *pIter0 = pStartTimeStamp->FirstChildElement("runID");
(gdb) next

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402ada in tinyxml2::XMLNode::FirstChildElement (this=0x0, value=0x408dd8 "runID") at tinyxml2.cpp:745
745     for( XMLNode* node=_firstChild; node; node=node->_next ) {

XML snippet

<mobileDevice>
        <mDeviceID></mDeviceID>
        <deviceDescription></deviceDescription>
        <units></units>
        <devicePlacement></devicePlacement>
        <quantisationResolution></quantisationResolution>
        <realTimeInformation>
            <runID/>
            <sampleRate/>
            <startTimeStamp/>
            <endTimeStamp/>
            <data/>
        </realTimeInformation>
        <event>
            <mEventID/>
            <timeStamp/>
            <data/>
            <support/>
        </event>
    </mobileDevice>

Solution

  • It seems that the following line is returning a null

    XMLElement *pStartTimeStamp = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("RealTimeInformation");
    

    This is evident by the gdb output saying that "this" pointer is null.