Julia GC wiping memory despite use of GC.@preserve

Hello fellow Julia users,

I realize that a small working example would be preferred, but I’m working within a fairly large project, and the behavior I’m seeing is not entirely repeatable. I was hoping to get some pointers just based on a description.

Basically I have a custom Julia package. I am trying to interface with this package from a custom C++ library. Basically I have the following call chain heirarchy

  1. C++ function calls highest level julia function as follows:
jl_value_t **gb_args;
JL_GC_PUSHARGS(gb_args, 9);
jl_value_t *input0= jl_box_float64(input0_cpp);
gb_args[0] = intput0;
.
.
gb_args[8] = intput8;

jl_value_t* inputs [9] = {input0, ..., input8}

// Call  the function
jl_call(my_jl_function_ptr, inputs,9);
JL_GC_POP();
  1. my_jl_function_ptr is a pointer to my_function_lv1(inputs) inside of julia
  2. my_function_lv1(inputs) calls my_function_lv2(inputs)
  3. my_function_lv2(inputs) call my_function_lv3(inputs)

Now the problem is sometimes everything works just fine, but sometimes one or more of the inputs at any of the levels get wiped. I’ve tried using GC.@preserve on all of the functions, but this doesn’t help since sometimes one of the inputs gets wiped from within the function my_function_lv3() before it gets used. The only solution I’ve had success with is within my_function_lv1() to do

GC.enable(false)
my_function_lv2()
GC.enable(true)

Is there a better way to do this than completely disabling the garbage collector like this each time I call my_function_lv2()?

Thanks!

1 Like