You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Michael Snebang <Mi...@post2.tele.dk> on 2001/08/15 12:57:47 UTC

Memory leak when parsing from a MemBufInputSource

In my program I am using a SAXParser and a MemBufInputSource to parse a XML
string. When I repeat this a number of times, it seems to use memory.

I am using Windows 2000, MS VC++ 6 sp. 4 and xerces 1.5.1.

I have reproduced the problem with the MemParse sample program:

If I add a loop around the parser creation and parse part, it uses apr. 1.2
K pr. itteration (based on Windows Task Manager figures)

for(int i=0;i<1000;i++)
{
    //
    //  Create a SAX parser object. Then, according to what we were told on
    //  the command line, set it to validate or not.
    //
    SAXParser parser;
    parser.setValidationScheme(valScheme);
    parser.setDoNamespaces(doNamespaces);
    parser.setDoSchema(doSchema);

    //
    //  Create our SAX handler object and install it on the parser, as the
    //  document and error handlers.
    //
    MemParseHandlers handler;
    parser.setDocumentHandler(&handler);
    parser.setErrorHandler(&handler);

    //
    //  Create MemBufferInputSource from the buffer containing the XML
    //  statements.
    //
    //  NOTE: We are using strlen() here, since we know that the chars in
    //  our hard coded buffer are single byte chars!!! The parameter wants
    //  the number of BYTES, not chars, so when you create a memory buffer
    //  give it the byte size (which just happens to be the same here.)
    //
    MemBufInputSource* memBufIS = new MemBufInputSource
    (
        (const XMLByte*)gXMLInMemBuf
        , strlen(gXMLInMemBuf)
        , gMemBufId
        , false
    );

    //
    //  Get the starting time and kick off the parse of the indicated
    //  file. Catch any exceptions that might propogate out of it.
    //
    unsigned long duration;
    try
    {
        const unsigned long startMillis =
XMLPlatformUtils::getCurrentMillis();
        parser.parse(*memBufIS);
        const unsigned long endMillis =
XMLPlatformUtils::getCurrentMillis();
        duration = endMillis - startMillis;
    }

    catch (const XMLException& e)
    {
        cerr << "\nError during parsing memory stream:\n"
             << "Exception message is:  \n"
             << StrX(e.getMessage()) << "\n" << endl;
        return -1;
    }

    // Print out the stats that we collected and time taken.
    cout << "\nFinished parsing the memory buffer containing the following "
         << "XML statements:\n\n"
         << gXMLInMemBuf
         << "\n\n\n"
         << "Parsing took " << duration << " ms ("
         << handler.getElementCount() << " elements, "
         << handler.getAttrCount() << " attributes, "
         << handler.getSpaceCount() << " spaces, "
         << handler.getCharacterCount() << " characters).\n" << endl;
}

If I change the code to parse from a file instead, there is no memory leaks.

Am I missing something, or is it a bug?

Michael Snebang
System Developer
Tele Denmark Communications


RE: Memory leak when parsing from a MemBufInputSource

Posted by Erik Rydgren <er...@mandarinen.se>.
Memory leak when parsing from a MemBufInputSourceTo me it looks like you do
a new without a delete.

HERE>>    MemBufInputSource* memBufIS = new MemBufInputSource

Erik Rydgren
Mandarinen systems AB
Sweden
  -----Original Message-----
  From: Michael Snebang [mailto:Michael.Snebang@post2.tele.dk]
  Sent: den 15 augusti 2001 12:58
  To: xerces-c-dev@xml.apache.org
  Subject: Memory leak when parsing from a MemBufInputSource




  In my program I am using a SAXParser and a MemBufInputSource to parse a
XML string. When I repeat this a number of times, it seems to use memory.

  I am using Windows 2000, MS VC++ 6 sp. 4 and xerces 1.5.1.

  I have reproduced the problem with the MemParse sample program:

  If I add a loop around the parser creation and parse part, it uses apr.
1.2 K pr. itteration (based on Windows Task Manager figures)

  for(int i=0;i<1000;i++)


      //
      //  Create a SAX parser object. Then, according to what we were told
on
      //  the command line, set it to validate or not.
      //
      SAXParser parser;
      parser.setValidationScheme(valScheme);
      parser.setDoNamespaces(doNamespaces);
      parser.setDoSchema(doSchema);

      //
      //  Create our SAX handler object and install it on the parser, as the
      //  document and error handlers.
      //
      MemParseHandlers handler;
      parser.setDocumentHandler(&handler);
      parser.setErrorHandler(&handler);

      //
      //  Create MemBufferInputSource from the buffer containing the XML
      //  statements.
      //
      //  NOTE: We are using strlen() here, since we know that the chars in
      //  our hard coded buffer are single byte chars!!! The parameter wants
      //  the number of BYTES, not chars, so when you create a memory buffer
      //  give it the byte size (which just happens to be the same here.)
      //
      MemBufInputSource* memBufIS = new MemBufInputSource


          (const XMLByte*)gXMLInMemBuf
          , strlen(gXMLInMemBuf)
          , gMemBufId
          , false
      );

      //
      //  Get the starting time and kick off the parse of the indicated
      //  file. Catch any exceptions that might propogate out of it.
      //
      unsigned long duration;
      try


          const unsigned long startMillis =
XMLPlatformUtils::getCurrentMillis();
          parser.parse(*memBufIS);
          const unsigned long endMillis =
XMLPlatformUtils::getCurrentMillis();
          duration = endMillis - startMillis;
      }

      catch (const XMLException& e)


          cerr << "\nError during parsing memory stream:\n"
               << "Exception message is:  \n"
               << StrX(e.getMessage()) << "\n" << endl;
          return -1;
      }

      // Print out the stats that we collected and time taken.
      cout << "\nFinished parsing the memory buffer containing the following
"
           << "XML statements:\n\n"
           << gXMLInMemBuf
           << "\n\n\n"
           << "Parsing took " << duration << " ms ("
           << handler.getElementCount() << " elements, "
           << handler.getAttrCount() << " attributes, "
           << handler.getSpaceCount() << " spaces, "
           << handler.getCharacterCount() << " characters).\n" << endl;
  }

  If I change the code to parse from a file instead, there is no memory
leaks.

  Am I missing something, or is it a bug?

  Michael Snebang
  System Developer
  Tele Denmark Communications