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 "Hal Black (JIRA)" <xe...@xml.apache.org> on 2007/05/19 06:54:16 UTC

[jira] Commented: (XERCESC-1508) createDocumentType does not release memory when document released/deleted

    [ https://issues.apache.org/jira/browse/XERCESC-1508?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel#action_12497119 ] 

Hal Black commented on XERCESC-1508:
------------------------------------

I ran a few more tests, but I think you guys have a handle on it.  Here's a quick summary:
Leak:
impl->createDocumentType()->release()

Leak:
impl->createDocument(0,0,impl->createDocumentType())->release()   [DOMDocumentType->release() *IS* called]

No Leak:
doc = impl->createDocument(0,0,0)
doc->createDocumentType()
doc->release()

Terminate:
doc1 = impl->createDocument(0,0,0)
docType = doc1->createDocumentType()
impl->createDocument(0,0,docType)

[I even tried to trick the above with cloneNode, to no avail.]

Segfault:
docType = impl->createDocument(0,0,0)
doc1 = impl->createDocument(0,0,docType)->release()
doc2 = impl->createDocument(0,0,docType)->release() (kaboom)

So it looks like there is some hybrid of keeping the part of the ownership of docType with the document, and part in the DOM implementation global document?  I looked at the source briefly and it was too invovled for me to figure out.

Anyone have any bright ideas for a workaround?

> createDocumentType does not release memory when document released/deleted
> -------------------------------------------------------------------------
>
>                 Key: XERCESC-1508
>                 URL: https://issues.apache.org/jira/browse/XERCESC-1508
>             Project: Xerces-C++
>          Issue Type: Bug
>          Components: DOM
>    Affects Versions: 2.6.0, 2.7.0
>         Environment: SUSE Linux 9, Red Hat Advanced Server 3.6, Ubuntu 7.04
>            Reporter: peter suggitt
>         Assigned To: Gareth Reakes
>         Attachments: DOMDocumentType_leak.cc, play.cpp
>
>
> Hi
> I am writing an aplication that has to create XML documents on the fly for interprocess communications.
> The problem that I have is that the memory is not being released when I use the DOMDocument (documentType) constructor.  I have played around with various different ways of releasing the memory including using the DOMDocumentType->release() method but alas no luck.  I appreciate that this may not seem like a huge issue but as I am looking memory everytime I create a new document I feel that this is an issue to be resolved.
> I have also found a few excepts on the web talking about this issue and there also seems to be a few people who believe this issue to be a case of bad API use:
> http://mail-archives.apache.org/mod_mbox/xerces-c-dev/200505.mbox/%3c87vf5bi9gy.fsf@openinformatics.com%3e
> .. this is followed up by a gentleman named Gareth Reakes from PathenonComputing describing that this is to do with the way that the release methods have not been called .. I do not believe him:
> "Nope, not a real leak. When you free the document the memory pools that were used to new things from are deleted so it cleans up all the things like the attribute list from the creation of an element."
> The below example shows how this is not the case.
> I have created the following example code thaqt fully replicates the issue including taking stats of the memory usage (RSS) after each document creation and deletion.  Please note that in main() I have a commented out version of the call to create the document as this shows how the standard constructor for the domdocument creates without any memory leaks
> ///////////////////////////////////////////
> #include <xercesc/util/PlatformUtils.hpp>
> #include <xercesc/parsers/XercesDOMParser.hpp> // to get the xmlstring
> #include <xercesc/dom/DOM.hpp>
> #include <iostream>
> #include <fcntl.h>
> #include <unistd.h>
> /////////////////////////////////////////////////////////////////////
> // pre decl
> XERCES_CPP_NAMESPACE::DOMDocument *createNewDoc();
> XERCES_CPP_NAMESPACE::DOMDocument *createNewDocWithType();
> int getMemorySize();
> /////////////////////////////////////////////////////////////////////
> int main()
> {
>     std::cout << "**************** starting" << std::endl;
>     XERCES_CPP_NAMESPACE::XMLPlatformUtils::Initialize();
>     for( int i = 0; i < 1000; ++i )
>     {
>         //XERCES_CPP_NAMESPACE::DOMDocument * doc = createNewDoc();
>         XERCES_CPP_NAMESPACE::DOMDocument * doc = createNewDocWithType();
>         std::cout << "    - Doing something with the document" << std::endl;
>         delete doc;
>         usleep(100000);
>         std::cout << "* Memory (rss): " << getMemorySize() << std::endl;
>     }
>     
>     XERCES_CPP_NAMESPACE::XMLPlatformUtils::Terminate();
>     std::cout << "**************** ending" << std::endl;
>     return 0;
> }
> /////////////////////////////////////////////////////////////////////
> XERCES_CPP_NAMESPACE::DOMDocument *createNewDocWithType()
> {
>     // set up strings
>     XMLCh *tName = XERCES_CPP_NAMESPACE::XMLString::transcode( "typeName" );
>     XMLCh *pub = XERCES_CPP_NAMESPACE::XMLString::transcode( "pubId" );
>     XMLCh *sys = XERCES_CPP_NAMESPACE::XMLString::transcode( "sysId" );
>     XMLCh *name = XERCES_CPP_NAMESPACE::XMLString::transcode( "rootName" );
>     XERCES_CPP_NAMESPACE::DOMImplementation *imp = XERCES_CPP_NAMESPACE::DOMImplementation::getImplementation();
>     // create the document type and the doc
>     XERCES_CPP_NAMESPACE::DOMDocumentType *type = imp->createDocumentType( tName, pub, sys );
>     XERCES_CPP_NAMESPACE::DOMDocument *tmp = imp->createDocument( NULL, name, type );
>     std::cout << "    - created a doc" << std::endl;
>     // clean up
>     XERCES_CPP_NAMESPACE::XMLString::release(&tName);
>     XERCES_CPP_NAMESPACE::XMLString::release(&name);
>     XERCES_CPP_NAMESPACE::XMLString::release(&pub);
>     XERCES_CPP_NAMESPACE::XMLString::release(&sys);
>     std::cout << "    - returning doc" << std::endl;
>     return tmp;;
> }
> /////////////////////////////////////////////////////////////////////
> XERCES_CPP_NAMESPACE::DOMDocument *createNewDoc()
> {
>     // set up strings
>     XERCES_CPP_NAMESPACE::DOMImplementation *imp = XERCES_CPP_NAMESPACE::DOMImplementation::getImplementation();
>     // create the document type and the doc
>     XERCES_CPP_NAMESPACE::DOMDocument *tmp = imp->createDocument();
>     std::cout << "    - returning doc" << std::endl;
>     return tmp;;
> }
> /////////////////////////////////////////////////////////////////////
> int getMemorySize()
> {
>     char fileName[64];
>     int fd;
>     snprintf( fileName, sizeof(fileName), "/proc/%d/statm", (int)getpid() );
>     fd = open(fileName, O_RDONLY);
>     if( fd == -1 )
>     {
>         return -1;
>     }
>     char memInfo[128];
>     int rval = read( fd, memInfo, sizeof(memInfo)-1 );
>     close(fd);
>     if( rval <= 0 )
>     {
>         return -1;
>     }
>     memInfo[rval] = '\0';
>     int rss;
>     rval = sscanf( memInfo, "%*d %d", &rss );
>     if( rval != 1 )
>     {
>         return -1;
>     }
>     return rss * getpagesize() / 1024;
> }
> /////////////////////////////////////////////////////////////////////

-- 
This message is automatically generated by JIRA.
-
You can reply to this email to add a comment to the issue online.


---------------------------------------------------------------------
To unsubscribe, e-mail: c-dev-unsubscribe@xerces.apache.org
For additional commands, e-mail: c-dev-help@xerces.apache.org