0

Is there any inherent memory leakage problem associated with tinyXML? I don't know why I am getting this strange seg fault. In the following function, whenever I try to increase the size of element name("Connecor" &"laneFro"), the following if clause(in the loop) causes the program to crash.

void WriteXMLInput_Node_Connectors(Node* node,TiXmlElement * Node_)
{
    std::ostringstream out;
    TiXmlElement * Connectors = new TiXmlElement("Connectors"); Node_->LinkEndChild(Connectors);
    TiXmlElement * Connector;
    TiXmlElement * laneFrom;
    TiXmlElement * laneTo;

    for(std::map<const sim_mob::Lane*, sim_mob::Lane* >::const_iterator it = node->getConnectors().begin();it != (node->getConnectors().end()); it++)
    {
        Connector = new TiXmlElement("Connecor"); Connectors->LinkEndChild(Connector);
        laneFrom = new TiXmlElement("laneFro"); Connector->LinkEndChild(laneFrom);
//      laneTo = new TiXmlElement("laneTo"); Connector->LinkEndChild(laneTo);
        if(42792 == node->getID())
        {
            std::cout << "uninode " ;
            std::cout << uninode->getID();
            std::cout << " from: " << (*it).first << " ";
            std::cout << (*it).first->getLaneID_str();
            std::cout << "uninode " << uninode->getID() << " To: " << (*it).second->getLaneID_str();

    }
}

Thank you

rahman
  • 4,820
  • 16
  • 52
  • 86
  • Have you checked e.g. in a debugger that all pointers are valid? – Some programmer dude Aug 07 '12 at 07:22
  • What does `getConnectors()` return, a reference or a value? If it is a value, `begin()` and `end()` would refer to different containers. – Bo Persson Aug 07 '12 at 07:24
  • @JoachimPileborg yes, in fact they work fine if I remove the tinyxml instructions – rahman Aug 07 '12 at 07:27
  • @BoPersson wow! the function was ment to provide a reference but I had missed the & in declaration and definition of function. I modified the function and now the program works. I don't understand how it is related?!! – rahman Aug 07 '12 at 07:28
  • 1
    @rahman - You are not the first one to do this. :-) If you return by value, you get a new copy each time so `begin()` and `end()` belongs to different containers and `it` will probably never get to the end. – Bo Persson Aug 07 '12 at 07:40
  • :) tanx would you put it as an answer to be marked as true? – rahman Aug 07 '12 at 07:43
  • @rahman: Undefined behaviour is undefined behaviour. Your program had an error and it's behaviour after that error is unpredictable. Your real mistake was thinking that TinyXML was at fault. 99.99% of the time it's the programmer not the library. Excellent spot by Bo Persson though. – john Aug 07 '12 at 08:34

0 Answers0