当我尝试执行代码时遇到段错误,它编译时没有错误/警告,但在执行时给出错误段错误
。下面是代码片段。
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>
最佳答案
以下行似乎返回 null
XMLElement *pStartTimeStamp = pRoot->FirstChildElement("mobileDevice")->FirstChildElement("RealTimeInformation");
gdb 输出表明“this”指针为空,这一点很明显。
关于C++ 段错误 -tinyXML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26382384/