Returning arbitrary Julia value from C function


I have a general question. Suppose I have a C function which creates and returns an arbitrary Julia value (e.g. an Array) as below:

jl_value_t* sample_func() {
  jl_value_t* value = nullptr;
  // code to create some Julia object and assign it to value
  // ...
  return value;

My question is, if I call the above function from julia using ccall, is there any guarantee that after JL_GC_POP() and before value's variable is assigned to a Julia variable this object won’t be garbage collected? If not, what’s the safe way to return arbitrary Julia values from a C function?


Yes as long as you call it with Any or Ref{<actual type>} return type in ccall.


Thank you!