Skip to content

Commit 1820227

Browse files
authored
do not reuse the context dictionary (fix #28) (#29)
1 parent 4d9fdf3 commit 1820227

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/document.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ Read `filename` and create an XML document.
139139
"""
140140
function readxml(filename::AbstractString)
141141
encoding = C_NULL
142-
options = 0
142+
# Do not reuse the context dictionary.
143+
options = 1 << 12
143144
doc_ptr = @check ccall(
144145
(:xmlReadFile, libxml2),
145146
Ptr{_Node},

src/node.jl

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -825,10 +825,12 @@ function unlink!(node::Node)
825825
Void,
826826
(Ptr{Void},),
827827
node.ptr)
828+
ccall(
829+
(:xmlSetTreeDoc, libxml2),
830+
Void,
831+
(Ptr{Void}, Ptr{Void}),
832+
node.ptr, C_NULL)
828833
update_owners!(node, node)
829-
# Unlinking must remove documents as well because
830-
# a node can free resources its document owns.
831-
unset_documents!(node)
832834
return node
833835
end
834836

@@ -874,16 +876,6 @@ function update_owners!(root, new_owner)
874876
end
875877
end
876878

877-
# Unset the .doc field of the `root` tree.
878-
function unset_documents!(root)
879-
offset = fieldoffset(_Node, 9)
880-
traverse_tree(root.ptr) do node_ptr
881-
unsafe_store!(
882-
convert(Ptr{UInt}, node_ptr + offset),
883-
convert(UInt, C_NULL))
884-
end
885-
end
886-
887879

888880
# Utils
889881
# -----

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,13 @@ end
971971
target = firstelement(root(doc2))
972972
@test_throws ArgumentError linknext!(target, child)
973973
@test_throws ArgumentError linkprev!(target, child)
974+
975+
# Issue #28
976+
doc = readxml(joinpath(dirname(@__FILE__), "sample1.xml"))
977+
unlink!(firstelement(root(doc)))
978+
# No need to check the return value because this is a bug of memory
979+
# management. Regression will be detected as a SIGABRT.
980+
@test true
974981
end
975982

976983
@testset "Validation" begin

0 commit comments

Comments
 (0)