Hi there,

for my job, an XML parser was implemented using xerces 2.4 (AIX 5.1), mais
we always had a memory leak.
Considering the complexity of the code, I tried to redesign a simple XML
parser using the 3.0.1. xerces version.
I reused a sample code from the xerces site "Dom programming guide" (-->
http://xerces.apache.org/xerces-c/program-dom-3.htm)
Here is my code (well, a part of it)

void bJTOTest::Traiter()
{
   XMLPlatformUtils::Initialize();
   for (int i = 0; i < 500; i++)
      creerFichierXML(i);
}

void bJTOTest::creerFichierXML(int p_iIndex)
{
   cout << "Traitement du fichier " << p_iIndex << endl;
   char tc_pid[10];
   cout << "MEMORY BEFORE" << endl;
   sprintf(tc_pid,"ps v %d", getpid());
   system(tc_pid);

   char l_tcFilePath[50+1];
   memset(l_tcFilePath, '\0', sizeof(l_tcFilePath));
   sprintf(l_tcFilePath, "XML/Test%d.xml", p_iIndex);

   // Doc XML
   DOMDocument* myDoc = NULL;
   XMLCh tempStr[100];
   //XMLString::transcode("LS", tempStr, 99);
   DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(XMLString::transcode("LS"));
   // create the document
   myDoc = impl->createDocument();

   cout << "    Création du noeud ID" << endl;
   DOMElement* firstChild;
   firstChild = myDoc->createElement(XMLString::transcode("array-list"));
   cout << "      appending the root node" << endl;
   myDoc->appendChild(firstChild);

   // XML Tree
   for (int i=0; i<30; i++)
   {
      char* l_pcMessage = NULL;
      try
      {
         cout << "New Indentity" << endl;

         cout << "    Création du noeud ID" << endl;
         DOMElement* firstChild;
         firstChild =
myDoc->createElement(XMLString::transcode("Indentity"));

            cout << "      Name" << endl;
            DOMElement* secondChild;
            secondChild =
myDoc->createElement(XMLString::transcode("Name"));
            DOMText* secondTextNode;
            secondTextNode =
myDoc->createTextNode(XMLString::transcode("WAZA"));
            secondChild->appendChild(secondTextNode);
            firstChild->appendChild(secondChild);

            cout << "      Firstname" << endl;
            DOMElement* thirdChild;
            thirdChild =
myDoc->createElement(XMLString::transcode("Firstname"));
            DOMText* thirdTextNode;
            thirdTextNode =
myDoc->createTextNode(XMLString::transcode("WAZA"));
            thirdChild->appendChild(thirdTextNode);
            firstChild->appendChild(thirdChild);

            cout << "      Gender" << endl;
            DOMElement* fourthChild;
            fourthChild =
myDoc->createElement(XMLString::transcode("Gender"));
            DOMText* fourthTextNode;
            fourthTextNode =
myDoc->createTextNode(XMLString::transcode("MALE"));
            fourthChild->appendChild(fourthTextNode);
            firstChild->appendChild(fourthChild);

         cout << "      appending the node in the tree" << endl;
         myDoc->getDocumentElement()->appendChild(firstChild);
      }
      catch(XMLException& XMLEx)
      {
         l_pcMessage = XMLString::transcode(XMLEx.getMessage());
         cout << "XMLException <" << l_pcMessage << endl;
         XMLString::release(&l_pcMessage);
      }
      catch(DOMException& DOMEx)
      {
         l_pcMessage = XMLString::transcode(DOMEx.msg);
         cout <<  "DOMException <" << l_pcMessage << "> code <" <<
DOMEx.code << ">" << endl;
         XMLString::release(&l_pcMessage);
      }
      catch(SAXException& SaxEx)
      {
         l_pcMessage = XMLString::transcode(SaxEx.getMessage());
         cout <<  "SAXException <" << l_pcMessage << endl;
         XMLString::release(&l_pcMessage);
      }
      catch(...)
      {
         cout << "OtherException <" << endl;
      }
   }

   cout << "Serialisation..." << endl;
   serializeDOM(myDoc, l_tcFilePath);

   cout << "MEMORY AFTERE" << endl;
   sprintf(tc_pid,"ps v %d", getpid());
   system(tc_pid);

   // on a fini avec le domcument, on libère la mémoire
   release(myDoc);
}

void bJTOTest::release(DOMDocument* myDoc)
{
   cout << "release" << endl;
   myDoc->release();
}

int bJTOTest::serializeDOM(DOMNode* node, char* path)
{
   XMLCh tempStr[100];
   XMLString::transcode("LS", tempStr, 99);
   DOMImplementation *impl =
DOMImplementationRegistry::getDOMImplementation(tempStr);
   DOMLSSerializer* theSerializer =
((DOMImplementationLS*)impl)->createLSSerializer();

   DOMLSOutput* theOutput = ((DOMImplementationLS*)impl)->createLSOutput();

   try
   {
      theSerializer->writeToURI(node, XMLString::transcode(path));
   }
   catch (const XMLException& toCatch)
   {
      char* message = XMLString::transcode(toCatch.getMessage());
      cout << "Exception message is: \n"
      << message << "\n";
      XMLString::release(&message);
      return -1;
   }
   catch (const DOMException& toCatch)
   {
      char* message = XMLString::transcode(toCatch.msg);
      cout << "Exception message is: \n"
      << message << "\n";
      XMLString::release(&message);
      return -1;
   }
   catch (...)
   {
      cout << "Unexpected Exception \n" ;
      return -1;
   }

   theOutput->release();
   theSerializer->release();
   return 0;
}

I release the serializer, the DOMLSOutput and the DOMDocument.
It says on the xerces Dom programming guide page, I quote :
"When a DOMDocument is released, all its associated children AND any objects
it owned (e.g. DOMRange, DOMTreeWalker, DOMNodeIterator or any orphaned
nodes) will also be released."
which I understand as : release a DOMDocumet and all its associated children
will also be release, right?

Nevertheless, I still have a memory leak, and I can't figure what am I doing
wrong...

Thx for your help : )
-- 
View this message in context: 
http://www.nabble.com/Xerces-3.0.1-C%2B%2B-memory-leak-tp23151106p23151106.html
Sent from the Xerces - C - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to