Returning arbitrary Julia value from C function


#1

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;
  JL_GC_PUSH!(&value);
  // code to create some Julia object and assign it to value
  // ...
  JL_GC_POP();
  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?


#2

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


#3

Thank you!