fooref = Ref(foo_object)
GC.@preserve fooref begin
ptr = Base.unsafe_convert(Ptr{Foo}, fooref)
....some code using ptr....
end
Here, I used GC.@preserve
to make sure that the fooref
object lives for as long as I need ptr
. Other strategies can also be employed (e.g. stashing fooref
in some persistent data structure) if you need ptr
to be valid for a longer time.