Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Memory leak #244

Closed
sdor opened this issue Mar 18, 2010 · 2 comments
Closed

Memory leak #244

sdor opened this issue Mar 18, 2010 · 2 comments

Comments

@sdor
Copy link

sdor commented Mar 18, 2010

Parsing ~1g xml file I have observed that ruby is using ~5g of memory.
Accidentally I looked at inner_xml and outer_xml functions

static VALUE inner_xml(VALUE self)
{
xmlTextReaderPtr reader;
Data_Get_Struct(self, xmlTextReader, reader);

const char * value = (const char *)xmlTextReaderReadInnerXml(reader);

if(value == NULL)
return Qnil;
else
return NOKOGIRI_STR_NEW2(value);
}

static VALUE outer_xml(VALUE self)
{
xmlTextReaderPtr reader;
Data_Get_Struct(self, xmlTextReader, reader);

const char * value = (const char *)xmlTextReaderReadOuterXml(reader);

if(value == NULL)
return Qnil;
else
return NOKOGIRI_STR_NEW2(value);
}

There is an obvious bug in these functions. xmlTextReaderReadOuterXml returns
non-constant xmlChar* which should be released via xmlFree. I had to create my own implementation of these functions in order to avoid memory leak.
Now I need just ~ 400 mb of memory.

Example of corrected inner_xml function

static VALUE inner_xml(VALUE self)
{
xmlTextReaderPtr reader;
Data_Get_Struct(self, xmlTextReader, reader);

xmlChar * value = xmlTextReaderReadInnerXml(reader);

if(value == NULL)
return Qnil;
else {
VALUE result= rb_str_new2((char*) value);
xmlFree(value);
return result;
}

}

@tenderlove
Copy link
Member

fixing memory leak in xml text reader. thanks sdor! closed by 5651e76

@tenderlove
Copy link
Member

Thanks for the bug report. We've fixed it here:

5651e76

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants