[xsd-users] memory leak in serializer.hxx

Boris Kolpackov boris at codesynthesis.com
Tue Jun 14 06:04:10 EDT 2011


Hi Mattia,

Mattia Tomasoni <tomasonimattia at googlemail.com> writes:

> The above is invoked over an over on all the nodes of my document: as those
> get written, the RAM starts filling up until the program is killed (for big
> enough files).
> If I comment the line:
> *e << x;
> no memory leak occurs and the output xml file is filled with empty elements
> (with the specified name and namespace).
> 
> My guess: "e" points to an area of memory that is modified (in some
> unorthodox way) by the "<<" operator and serialized to file. Once the job is
> done, a delete on the pointer "e" will cause a memory fault. To obviate the
> problem, codesynthesis created their own version of an auto_ptr that returns
> responsibility to the programmer (by using release) instead of freeing the
> memory.

No, this is now what happens. dom::auto_ptr does release the DOM node.
However, in Xerces-C++, the memory used by such a node is not freed
immediately. Rather, the DOMDocument instance maintains its own heap
of such memory blocks which it tries to reuse later.

With this logic it is possible to have a situation where DOM cannot
reuse any of the existing blocks and keeps allocating more and more
memory. For example, if your application progressively serializes
bigger and bigger elements, then DOM won't be able to reuse any of
the existing memory blocks and will be forced to continuously allocate
new ones. This situation is unlikely but possible.

One way to resolve this is to free the DOMDocument instance after a
certain number of elements have been serialized. This will force the
freeing of all the allocated memory. Here is how you can do this:

1. Add the "size_t count_;" member after doc_ in the serializer_impl
   class in serializer.cxx.

2. Initialize it to 0 in the serializer_impl constructor.

3. Modify the two create() functions by adding the following code
   at the beginning of each of them:

   if (count_++ > 1000)
   {
     doc_.reset (dom_impl_.createDocument ());
     count_ = 0;
   }

Boris



More information about the xsd-users mailing list