Skip to content

Commit

Permalink
Fix reading references if a previously read reference is gced
Browse files Browse the repository at this point in the history
  • Loading branch information
simonster committed Aug 22, 2014
1 parent 6714b2e commit 376ccd4
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/JLD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,15 @@ end

## Reference
function read_ref(f::JldFile, ref::HDF5ReferenceObj)
haskey(f.jlref, ref) && return f.jlref[ref].value
if haskey(f.jlref, ref)
# Objects are stored as WeakRefs, and may no longer exist
val = f.jlref[ref].value
if val == nothing
delete!(f.jlref, ref)
else
return val
end
end

dset = f[ref]
data = try
Expand Down
21 changes: 21 additions & 0 deletions test/jld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,27 @@ for mmap = (true, false)
close(fidr)
end

# object references in a write session
x = ObjRefType()
a = [x, x]
b = [x, x]
@save fn a b
jldopen(fn, "r") do fid
a = read(fid, "a")
b = read(fid, "b")
@test a[1] === a[2] === b[2] === a[1]

# Let gc get rid of a and b
a = nothing
b = nothing
gc()

a = read(fid, "a")
b = read(fid, "b")
@test typeof(a[1]) == ObjRefType
@test a[1] === a[2] === b[2] === a[1]
end

# do syntax
jldopen(fn, "w") do fid
g_create(fid, "mygroup") do g
Expand Down

0 comments on commit 376ccd4

Please sign in to comment.