Re: memory leak in libxml2 - fix

Tom Lane <tgl@sss.pgh.pa.us>

From: Tom Lane <tgl@sss.pgh.pa.us>
To: Pavel Stehule <pavel.stehule@gmail.com>
Cc: Itagaki Takahiro <itagaki.takahiro@gmail.com>, PostgreSQL Hackers <pgsql-hackers@postgresql.org>
Date: 2010-11-26T19:14:08Z
Lists: pgsql-hackers

Attachments

Pavel Stehule <pavel.stehule@gmail.com> writes:
> 2010/11/26 Itagaki Takahiro <itagaki.takahiro@gmail.com>:
>> Why did you change doctree and ctxt to global variables?
>> I'm not sure why /* xmlFreeDoc(doctree); */ is commented out
>> at the end of pgxml_xpath(), but is it enough to enable the code?

> I am thinking, so you must not to call xmlFreeDoc(doctree) early.
> Probably xmlXPathCastToXXX reading a doctree.

Those static variables are really ugly, and what's more this patch only
stops some of the leakage.  Per experimentation, the result object from
pgxml_xpath has to be freed too, once it's been safely converted to
whatever the end result type is.  You can see this by watching

select sum(xpath_number('<data>' || generate_series || '</data>','/data')) from
generate_series(1,500000);

which still shows leakage with the submitted patch.  I cleaned it up
as per attached, which doesn't show any leakage.

			regards, tom lane