C ++分段错误 - tinyXML

时间:2014-10-15 12:15:51

标签: c++ linux segmentation-fault tinyxml

我在尝试执行代码时遇到分段错误,它在没有错误/警告的情况下进行编译,但在执行时会出现错误Segmentation fault。以下是下面的代码段。

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);

}

编辑: 我尝试运行gdb,但结果是

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 ) {

当单步执行代码时,它会在此行失败:

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代码段

<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>

2 个答案:

答案 0 :(得分:0)

我打赌其中一个是空的:

 pRoot
 pRoot->FirstChildElement("mobileDevice")
 pStartTimeStamp
 pNewStartTimeStamp

那么如果你追踪出来找出哪一个怎么样呢。

答案 1 :(得分:0)

似乎以下行返回null

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

gdb输出显示“this”指针为空。