GC.@preserve missing_bytes res begin
src_ptr = Ptr{UInt8}(pointer(missing_bytes))
dest_ptr = Ptr{UInt8}(pointer(res, res_len+1)) + from - 1
# copy content over
unsafe_copyto!(dest_ptr, src_ptr, res_len)
end
I am getting an error, but I want to check my understanding. In the code above, I wanted to copy some memory over from one location to another. So to do that I don’t want the memory location to change, so I preserve missing_bytes and res so that their memory location will not change for duration of the begin and end block. Is my understand correct? So if the code fails, it must be because of either
my operation is illegal
There is Julia bug (highly unlikely)
and I can rule the data having moved location while that code is running causing issues. Right?
GC.@preserve, or the GC in general does nothing about the “location” of variables/arrays, it makes sure the object is valid and all derived pointer from it are valid unless explicitly invalidated otherwise, for example, if you push an element to an array. For well defined object the use of GC.@preserve looks correct assuming no race or other undefined behavior (e.g. out of bound access).
I am looking at Vector{Union{Missing, T}}. The missing indicators are located past the T values, hence looking past that in this is the right way to go.
so basically it just ensures my pointer calls will return the location I wanted. Anyway, have not encountered any memory crashes after I fixed the bugs, so my understanding of GC.@preserve, although wrong didn’t lead me astray.
I’m pretty sure it may not be there. There could be gap in between. (Also I thought there’s a function to give you it’s location so you don’t need to do it yourself)
I see it. It’s hard to interpret how I can use it in practice.
My use case is that I know where the missing are but the data I want to copy are not in the form of a complete Vector{Union{Missing, T}}. But code just shows how to use them with Vector{Union{T, Missing}} which is via a ccall to jl_array_typetagdata. So I can’t go deeper into jl_array_typetagdata.