How do I push an array to the garbage collector in C?

So on https://docs.julialang.org/en/stable/manual/embedding/ it says how to use Julia arrays and the garbage collector in C, but not how to use them together.

Let’s say, I allocated an array in the following manner as it says in the example:

jl_value_t* array_type = jl_apply_array_type((jl_value_t*)jl_float64_type, 1);
jl_array_t* x          = jl_alloc_array_1d(array_type, 10);
double *xData          = (double*)jl_array_data(x);

If I do not use the array in the next call to any jl_ function, it will be garbage collected, as evidenced by (a) it not giving me a garbage value with jl_gc_enable(0) and (b) it also working fine if I use the array in Julia in the next call.

I could push the array to the garbage collector, but the example code below is not helping me:

jl_value_t **args;
JL_GC_PUSHARGS(args, 2); // args can now hold 2 `jl_value_t*` objects
args[0] = some_value;
args[1] = some_other_value;
// Do something with args (e.g. call jl_... functions)
JL_GC_POP();

This code requires a jl_value_t**, but I don’t have such an array. I only have the array_type, x and xData, which are all three of a different type.

So how would I go about pushing the array so that it does not get garbage collected?

Even if you do, it could still be collected if you didn’t root it. The jl_call... functions are the exceptions to the rule to make it slightly easier to use (though more error-prone I would say).

You have such an array, it’s args. It stores pointers to julia object. Most (all?) jl_..._t you have access of are julia object and you just need to cast the pointer. In this case, array_type is a leaf type so you don’t necessarily have to root it. You could also use one of JL_GC_PUSH<n> to treat a local variable as root instead of explicitly storing the object to the stack frame everytime.

Okay, thanks for your help!

So I again have to box my array contents with the boxing functions? Because right now I am just adding them to xData without any boxing.

What do you mean by box your array content? x is the box for xData. Unless required by the API, you shouldn’t need to box anything.

Yes, thank you. I have looked into boxing and I now understand it. Thanks for your help!