Skip to content

Commit 905d839

Browse files
committed
Fix: keep ref to Node in XML::XPathContext
We need the document Node, but we also connect the Node to the xpath context, so we should retain the ref to Node that will indirectly ref the document Node.
1 parent a4ed7d2 commit 905d839

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

src/xml/xpath_context.cr

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
class XML::XPathContext
22
getter errors = [] of XML::Error
33

4-
def initialize(node : Node)
5-
@ctx = LibXML.xmlXPathNewContext(node.to_unsafe.value.doc)
6-
@ctx.value.node = node.to_unsafe
7-
@document = node.document
4+
def initialize(@node : Node)
5+
@ctx = LibXML.xmlXPathNewContext(@node.to_unsafe.value.doc)
6+
@ctx.value.node = @node.to_unsafe
87

98
{% if LibXML.has_method?(:xmlXPathSetErrorHandler) %}
109
LibXML.xmlXPathSetErrorHandler(@ctx, ->Error.structured_callback, Box.box(@errors))
@@ -45,12 +44,12 @@ class XML::XPathContext
4544
when LibXML::XPathObjectType::NODESET
4645
if xpath_object.value.nodesetval
4746
# don't free xpath object: that would free the nodeset
48-
return NodeSet.new(@document, xpath_object)
47+
return NodeSet.new(@node.document, xpath_object)
4948
end
5049
end
5150

5251
LibXML.xmlXPathFreeObject(xpath_object)
53-
retval || NodeSet.new(@document)
52+
retval || NodeSet.new(@node.document)
5453
end
5554

5655
def register_namespaces(namespaces) : Nil

0 commit comments

Comments
 (0)