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 Michiaki HAMADA / FUJI-RIC <mh...@star.fuji-ric.co.jp> on 2003/06/06 04:53:53 UTC

How to release memory

Hello everyone.

By seeing unix "top" command,
I found out the thing that memories kept increasing 
in my sample program (which is mentioned in the end
of this mail). 

By the way I am using xerces-c++ verion 2.2.0 on 
Redhat Linux 7.3.

Where is wrong in my program ?
 
------ sample program starts --------

#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/util/PlatformUtils.hpp>

using namespace std;
using namespace xercesc;

#define X(x) XMLString::transcode(x)

int main() {
  for (int i=0;i<5000000;i++) {
    XMLPlatformUtils::Initialize();
    {
      DOMImplementation* impl =
 DOMImplementationRegistry::getDOMImplementation(X(""));
      DOMDocument* doc = impl->createDocument( X("aaaaa" ), 
            X("bbbbb:ccccc"), NULL );
      DOMElement* drugMLElement = doc->getDocumentElement();
      drugMLElement->setAttribute( X("dddddd"),
       X("http://eeeee") );
      doc->release();

    }
    XMLPlatformUtils::Terminate();
  }
  return 1;
}

------------
M. Hamada

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


Re: How to release memory

Posted by Michiaki HAMADA / FUJI-RIC <mh...@star.fuji-ric.co.jp>.
Hello David,

My problem is soloved perfectly.
Thank you very much for your kind advise.

----- Original Message ----- 
From: "David J Craigon" <da...@arabidopsis.info>
To: <xe...@xml.apache.org>
Sent: Friday, June 06, 2003 7:18 PM
Subject: Re: How to release memory


> Hello Hamada-san,
> XMLString::transcode produces a string which belongs to you, and you
> need to delete after use. So if you use something like:
> 
> DOMDocument* doc = impl->createDocument( XMLString::transcode("aaaaa" ),
> XMLString::transcode("bbbbb:ccccc"), NULL );
> 
> the two calls to XMLString::transcode leak memory, since they create a
> string which is passed to the function, but it is never deallocated.
> 
> In this case, it you can use delete[], but in line with other Xerces-C
> practise, you are better off using XMLString::release().
> 
> What you need is something like:
> #include <xercesc/dom/DOM.hpp>
> #include <xercesc/util/XMLString.hpp>
> #include <xercesc/dom/DOMImplementation.hpp>
> #include <xercesc/dom/DOMImplementationRegistry.hpp>
> #include <xercesc/util/PlatformUtils.hpp>
> 
> using namespace std;
> using namespace xercesc;
> 
> #define X(x) XMLString::transcode(x)
> 
> int main() {
>   for (int i=0;i<5000000;i++) {
>     XMLPlatformUtils::Initialize();
>     {
>       XMLCh *temp1;
>       XMLCh *temp2;
> 
>       temp1=X("");
>       DOMImplementation* impl =
> DOMImplementationRegistry::getDOMImplementation(temp1);
>       XMLString::release(&temp1);
> 
>       temp1=X("aaaaa" );
>       temp2=X("bbbbb:ccccc");
>       DOMDocument* doc = impl->createDocument( temp1,
>        temp2, 0);
>       XMLString::release(&temp1);
>       XMLString::release(&temp2);
> 
>       DOMElement* drugMLElement = doc->getDocumentElement();
> 
>       temp1=X("dddddd");
>       temp2=X("http://eeeee");
>       drugMLElement->setAttribute( temp1,temp2);
>       XMLString::release(&temp1);
>       XMLString::release(&temp2);
>       doc->release();
> 
>     }
>     XMLPlatformUtils::Terminate();
>   }
>   return 1;
> }
> 
> This compiles for me without similar problems.
> 
> David
> 
> 
> Michiaki HAMADA / FUJI-RIC wrote:
> > Hello everyone.
> > 
> > By seeing unix "top" command,
> > I found out the thing that memories kept increasing 
> > in my sample program (which is mentioned in the end
> > of this mail). 
> > 
> > By the way I am using xerces-c++ verion 2.2.0 on 
> > Redhat Linux 7.3.
> > 
> > Where is wrong in my program ?
> >  
> > ------ sample program starts --------
> > 
> > #include <xercesc/dom/DOM.hpp>
> > #include <xercesc/util/XMLString.hpp>
> > #include <xercesc/dom/DOMImplementation.hpp>
> > #include <xercesc/dom/DOMImplementationRegistry.hpp>
> > #include <xercesc/util/PlatformUtils.hpp>
> > 
> > using namespace std;
> > using namespace xercesc;
> > 
> > #define X(x) XMLString::transcode(x)
> > 
> > int main() {
> >   for (int i=0;i<5000000;i++) {
> >     XMLPlatformUtils::Initialize();
> >     {
> >       DOMImplementation* impl =
> >  DOMImplementationRegistry::getDOMImplementation(X(""));
> >       DOMDocument* doc = impl->createDocument( X("aaaaa" ), 
> >             X("bbbbb:ccccc"), NULL );
> >       DOMElement* drugMLElement = doc->getDocumentElement();
> >       drugMLElement->setAttribute( X("dddddd"),
> >        X("http://eeeee") );
> >       doc->release();
> > 
> >     }
> >     XMLPlatformUtils::Terminate();
> >   }
> >   return 1;
> > }
> > 
> > ------------
> > M. Hamada
> > 
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> > For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> > 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 
> 

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


Re: How to release memory

Posted by David J Craigon <da...@arabidopsis.info>.
Hello Hamada-san,
	XMLString::transcode produces a string which belongs to you, and you
need to delete after use. So if you use something like:

DOMDocument* doc = impl->createDocument( XMLString::transcode("aaaaa" ),
XMLString::transcode("bbbbb:ccccc"), NULL );

the two calls to XMLString::transcode leak memory, since they create a
string which is passed to the function, but it is never deallocated.

In this case, it you can use delete[], but in line with other Xerces-C
practise, you are better off using XMLString::release().

What you need is something like:
#include <xercesc/dom/DOM.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/dom/DOMImplementation.hpp>
#include <xercesc/dom/DOMImplementationRegistry.hpp>
#include <xercesc/util/PlatformUtils.hpp>

using namespace std;
using namespace xercesc;

#define X(x) XMLString::transcode(x)

int main() {
  for (int i=0;i<5000000;i++) {
    XMLPlatformUtils::Initialize();
    {
      XMLCh *temp1;
      XMLCh *temp2;

      temp1=X("");
      DOMImplementation* impl =
	DOMImplementationRegistry::getDOMImplementation(temp1);
      XMLString::release(&temp1);

      temp1=X("aaaaa" );
      temp2=X("bbbbb:ccccc");
      DOMDocument* doc = impl->createDocument( temp1,
					       temp2, 0);
      XMLString::release(&temp1);
      XMLString::release(&temp2);

      DOMElement* drugMLElement = doc->getDocumentElement();

      temp1=X("dddddd");
      temp2=X("http://eeeee");
      drugMLElement->setAttribute( temp1,temp2);
      XMLString::release(&temp1);
      XMLString::release(&temp2);
      doc->release();

    }
    XMLPlatformUtils::Terminate();
  }
  return 1;
}

This compiles for me without similar problems.

David


Michiaki HAMADA / FUJI-RIC wrote:
> Hello everyone.
> 
> By seeing unix "top" command,
> I found out the thing that memories kept increasing 
> in my sample program (which is mentioned in the end
> of this mail). 
> 
> By the way I am using xerces-c++ verion 2.2.0 on 
> Redhat Linux 7.3.
> 
> Where is wrong in my program ?
>  
> ------ sample program starts --------
> 
> #include <xercesc/dom/DOM.hpp>
> #include <xercesc/util/XMLString.hpp>
> #include <xercesc/dom/DOMImplementation.hpp>
> #include <xercesc/dom/DOMImplementationRegistry.hpp>
> #include <xercesc/util/PlatformUtils.hpp>
> 
> using namespace std;
> using namespace xercesc;
> 
> #define X(x) XMLString::transcode(x)
> 
> int main() {
>   for (int i=0;i<5000000;i++) {
>     XMLPlatformUtils::Initialize();
>     {
>       DOMImplementation* impl =
>  DOMImplementationRegistry::getDOMImplementation(X(""));
>       DOMDocument* doc = impl->createDocument( X("aaaaa" ), 
>             X("bbbbb:ccccc"), NULL );
>       DOMElement* drugMLElement = doc->getDocumentElement();
>       drugMLElement->setAttribute( X("dddddd"),
>        X("http://eeeee") );
>       doc->release();
> 
>     }
>     XMLPlatformUtils::Terminate();
>   }
>   return 1;
> }
> 
> ------------
> M. Hamada
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 



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