Re: [xml] memory leak: attribute values are not freed when exception is thrown in the startElement callback

On 4/10/2014 16:53, Bart De Schuymer wrote:

On Thu, Apr 10, 2014 at 2:30 PM, Nikolay Sivov <bunglehead gmail com> wrote:
On 4/10/2014 16:20, Bart De Schuymer wrote:

We tried using libxml2 configured with the --with-fexceptions option and have identified a memory leak when our C++ startElement callback throws an exception.
The cause of the leak is as follows:
xmlParseStartTag frees all attribute values after calling the startElement callback, but since an exception is thrown in the callback, this cleanup code isn't executed.
xmlFreeParserCtxt only frees ctxt->atts: it doesn't try to free any leftover attribute values.

We tried to patch libxml2 to fix this issue but ran into errors when running make valgrind ("Memory tag error occurs"). So we decided to catch the exceptions in our callback instead which effectively prevents the memory leak from occurring.
Yes, you should handle that on your side. libxml2 is a C library, so you can't handle cpp features in it, you could try of course to maintain your own version of libxml2 extended with exception handler wrappers for all callbacks you need, but it's a strange thing to do.

I see. If that's the official statement then it is a bad idea to offer the --with-fexceptions flag at configuration time. Especially since there is no mention of the accompanying memory leaks.
No, I'm not speaking officially and I'm not involved in libxml2 development, this is just my opinion. I mean it seems natural to me that cpp specific features won't work for C lib, as another commenter mentioned you might consider cpp wrappers with corresponding exception support.

Best regards,

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