Menu

Detatch Document / Node?

2005-06-30
2013-05-20
  • Georg Grabler

    Georg Grabler - 2005-06-30

    Hello everyone.

    I've a small problem concerning reading / appending to a document.

    I've an existing TiXmlDocument, and get (socket transfer) another XML file, which i need to append in a special part in the tree.

    Currently, i'm using another document to parse the incoming string, appending it to the "real" document.

    Now, i have a memory leak everyone knows: I append the RootNode to the "real" document. The temporary document still exists. Now, when i delete the temporary document, i destroy the real document tree. When i dont delete the temporary document, i have created a new memory leak...

    Any suggestions?

     
    • Georg Grabler

      Georg Grabler - 2005-06-30

      In between i have implemented the following experimental function:

      void TiXmlNode::Detach()
      {
          next->prev = prev;
          prev->next = next;
          return this;
      }

      This basically works. It detaches any node in the tree (till now quite properly as far as i could test that). So i can just link the node i get returned (the node which got detached of the other document) to my 2nd doc.

       
    • Ellers

      Ellers - 2005-07-01

      Can you reproduce/show the problem in a small block of code?
      Cheers
      Ellers

       
    • Georg Grabler

      Georg Grabler - 2005-07-01

      TiXmlDocument *doc = new TiXmlDocument();
          TiXmlNode *response = doc->LinkEndChild (new TiXmlElement ("response"));
          response->ToElement()->SetAttribute ("type", "save");
          response->ToElement()->SetAttribute ("requestid", requestid);
          TiXmlNode *status = response->LinkEndChild (new TiXmlElement ("status"));

      status->LinkEndChild (new TiXmlText ("OK"));
                  TiXmlNode* pageid = response->LinkEndChild (new TiXmlElement ("pageid"));
                  pageid->ToElement()->SetAttribute("old",pageID);
                  pageid->ToElement()->SetAttribute("db",lastid);
      TiXmlDocument *tmpdoc = new TiXmlDocument();
                          tmpdoc->Parse (x.c_str());

                          if (string(tmpdoc->RootElement()->FirstChild("status")->FirstChild()->Value()) != "failed")
                          {
                              pageid->LinkEndChild (tmpdoc->RootElement()->FirstChild("status")->NextSibling("articleid")->Detach());
                              delete tmpdoc;
                          }

      -----------------------

      TiXmlNode* TiXmlNode::Detach()
      {
          if (next)
              next->prev = prev;
          if (prev)
              prev->next = next;
          if (parent->FirstChild() == this)
              parent->firstChild=next;
          return this;
      }

      -------------

      Basically, i need to delete a party of the document i get back from tmpstr, since i dont want to append all the document. So i need to detach it, deleting new document.

       

Log in to post a comment.