-
-
Notifications
You must be signed in to change notification settings - Fork 905
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 #856
Comments
Yeargh, this causes a segfault on 1.8.7. Reverting for now, I'll revisit when I have a bit more time to dig in. |
The leak can be whittled down to:
|
@ender672 take a look at my reverted commit above. It crashed on 1.8.7 but was fine (even under Valgrind) on 1.9.3 and 2.0.0. The issue is partly that the fragment nodes, if left unparented, are not GCed as the document doesn't know they exist. We've got to figure out a way to make sure top-level fragment nodes are known by the document; I thought my patch addressed it, but did not have time to investigate why 1.8.7 segfaulted (double-free). Can you investigate? |
Your solution looks correct, but it exposes a subtle nuance in nokogiri_root_node() and libxml2's xmlAddChild() function. When Nokogiri frees a document, it goes through the list of nodes marked by nokogiri_root_node() and adds them to the document root via xmlAddChild() so that xmlFreeDoc() takes care of them later. In ext/nokogiri/xml_document.c:13
Interestingly, xmlAddChild() will not touch the When nokogiri creates the new nodes in in_context(), the new nodes are linked via the I think the best solution is to set the
However, I'm a little nervous about doing this in an rc release. There is a possibility that nokogiri is taking advantage of this quirk and that setting node->next to NULL will expose a new memory leak. |
After taking some time to look at the users of nokogiri_root_node(), I feel a lot more comfortable with this fix. In all of Nokogiri's unit tests, node->next is always NULL, with the exception of nodes added via in_context(). I feel we can safely add this to the rc. Any concerns? |
The memory leak test that have infinite loops eventually should be moved out of the unit test framework. In the meantime, you can test the one related to this ticket with:
|
Sorry for not responding to your earlier comments. This looks good! I'm going to cut another bugfix release today (for this and other issues). |
Reproduced under:
Here's the repro script from @armhold:
The text was updated successfully, but these errors were encountered: