Re: [xml] Memory leak / cleanup missing after node creation.

On 25.05.2004 16:31, Jose Commins wrote:

I see! So in effect xmlBufferCreateSize does indeed allocate the amount of memory I specify (my xml output buffers never go beyond 4K, so I allocate 8192 just in case).

Don't worry about the size. As soon as the data is larger than the buffer, the buffer will grow but it will keep its new size. If the same data arrives in a future pass, it will fit into the buffer this time. A call to, for instance xmlBufferEmpty, will not restore the buffer's initial size.

In my app I keep the buffers persistent throughout threads, so I don't constantly create/destroy buffers, just re-use them on the next pass. Seeing as I have to use xmlBufferEmpty or xmlBufferShrink after each use, am I right in thinking that xmlBufferShrink is more optimal since it doesn't clear the data (I am guessing here)?

No. xmlBufferShrink will remove data from the beginning of the buffer. It will not trim the unused memory from its end, as you might be led to believe. Unless you are working with large data chunks which cannot fit in your computer's memory, you don't need this function.

You should do what you did, call xmlBufferEmpty when you are done working with the data in the buffer. This will empty the buffer, but keep the allocated memory. Without calling xmlBufferEmpty, your each pass would be appending data to what the buffer contained in the previous pass. xmlNodeDump appends data to the buffer rather than replacing it, of course. How else would its recursive call on node's children work?

By the way, I have to reiterate Igor that you have been of invaluable help and knowledge on these matters and I send my thanks again.

A thank expressed in cold beer pitchers is most welcome. :-)


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]