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

replace unsafe pointer operations with their gc-safe Ref equivalents #428

Merged
merged 4 commits into from
Jul 28, 2017

Conversation

vtjnash
Copy link
Contributor

@vtjnash vtjnash commented Jul 17, 2017

Avoids using pointer so that Julia can natively track the usage of the objects and ensure it is gc-rooted.

h5t_set_size(memtype_id, H5T_VARIABLE)
readarray(obj, memtype_id, buf)
# FIXME? Who owns the memory for each string? Will Julia free it?
# FIXME? Who owns the memory for each string? Julia v0.6+ won't free it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this comment still valid ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to double check, do you know whether this comment still applies with your fix? If not no problem just wanted to know if this #FIXME can be removed or not. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only answered one of the questions there. I suspect that implies the answer the the first question (is that this is a memory leak and we should be manually freeing the memory here after the call to unsafe_string), but perhaps instead HDF5 owns the memory (as a pointer into the mmap'd file?) and attempting to free it would immediately cause it to crash.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking the docs, it appears there may supposed to be a call to H5Dvlen_reclaim before the call to h5t_close (and this function should perhaps be wrapped in try/catch so those get called even if there's an error with reading part of the data.

src/HDF5.jl Outdated
close, convert, done, eltype, endof, flush, getindex, ==,
isempty, isvalid, length, names, ndims, next, parent, read,
setindex!, show, size, sizeof, start, write, isopen

import Compat: StringVector
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be using since StringVector not extended.

end
p = Ref{Cstring}(nonnull_str)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if str = ["\0","a","b"]
then

julia> p = Ref{Cstring}(nonnull_str)
ERROR: ArgumentError: embedded NULs are not allowed in C strings: "\0"

Is this expected?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, HDF5 appears to only support C-type strings (the string "\0" will get truncated to ""). The error message seems pretty self-explanitory to me? 🙂

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I wasn't clear. I meant to say that with this PR the following doesn't work (i.e. the "\0" doesn't get truncated to "")

using HDF5
fn = tempname()
f = h5open(fn, "w")
test = ["s","dfs","Hi there","\0"]
write(f, "test", test)
ERROR: ArgumentError: embedded NULs are not allowed in C strings: "\0"

whereas on master it truncates the last element of test to ""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, the last element should be "". Why would you ever want it to silently discard / truncate your data? :)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sry just wanted to confirm the behavior change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Np, happy to confirm

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't there still an inconsistency here
For arrays with "\0" it will throw an ArgumentError, however if you just try to write a string with "\0" it will still truncate it to "" on this PR.

using HDF5
fn = tempname()
f = h5open(fn, "w")
write(f, "test", "\0")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File another bug?

@musm musm merged commit e4052c9 into JuliaIO:master Jul 28, 2017
@vtjnash vtjnash deleted the jn/gcsafe branch July 29, 2017 05:12
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

Successfully merging this pull request may close these issues.

2 participants