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

Add GC preserve to unsafe_string conversions from pointer #23

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions src/CFITSIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -193,21 +193,23 @@ function Base.showerror(io::IO, c::CFITSIOError)
end
end

tostring(v) = GC.@preserve v unsafe_string(pointer(v))

function fits_get_errstatus(status::Cint)
msg = Vector{UInt8}(undef, 31)
ccall((:ffgerr, libcfitsio), Cvoid, (Cint, Ptr{UInt8}), status, msg)
unsafe_string(pointer(msg))
tostring(msg)
end

function fits_read_errmsg()
msg = Vector{UInt8}(undef, 80)
msgstr = ""
ccall((:ffgmsg, libcfitsio), Cvoid, (Ptr{UInt8},), msg)
msgstr = unsafe_string(pointer(msg))
msgstr = tostring(msg)
errstr = msgstr
while msgstr != ""
ccall((:ffgmsg, libcfitsio), Cvoid, (Ptr{UInt8},), msg)
msgstr = unsafe_string(pointer(msg))
msgstr = tostring(msg)
errstr *= '\n' * msgstr
end
return errstr
Expand Down Expand Up @@ -469,7 +471,7 @@ function fits_file_name(f::FITSFile)
status,
)
fits_assert_ok(status[])
unsafe_string(pointer(value))
tostring(value)
end

"""
Expand Down Expand Up @@ -538,7 +540,7 @@ function fits_read_key_str(f::FITSFile, keyname::String)
status,
)
fits_assert_ok(status[])
unsafe_string(pointer(value)), unsafe_string(pointer(comment))
tostring(value), tostring(comment)
end

function fits_read_key_lng(f::FITSFile, keyname::String)
Expand All @@ -557,7 +559,7 @@ function fits_read_key_lng(f::FITSFile, keyname::String)
status,
)
fits_assert_ok(status[])
value[], unsafe_string(pointer(comment))
value[], tostring(comment)
end

function fits_read_keys_lng(f::FITSFile, keyname::String, nstart::Integer, nmax::Integer)
Expand Down Expand Up @@ -604,7 +606,7 @@ function fits_read_keyword(f::FITSFile, keyname::String)
status,
)
fits_assert_ok(status[])
unsafe_string(pointer(value)), unsafe_string(pointer(comment))
tostring(value), tostring(comment)
end


Expand All @@ -628,7 +630,7 @@ function fits_read_record(f::FITSFile, keynum::Integer)
status,
)
fits_assert_ok(status[])
unsafe_string(pointer(card))
tostring(card)
end


Expand Down Expand Up @@ -656,9 +658,9 @@ function fits_read_keyn(f::FITSFile, keynum::Integer)
)
fits_assert_ok(status[])
(
unsafe_string(pointer(keyname)),
unsafe_string(pointer(value)),
unsafe_string(pointer(comment)),
tostring(keyname),
tostring(value),
tostring(comment),
)
end

Expand Down
Loading