Well, I am going to describe what I did here, because I am finding very difficult to obtain clear information about the process of embedding Julia into C++ (when I get better, I will submit a PR to improve the documentation).
(Please, correct me if I am wrong!)
In fact, Julia will not GCed a variable if it is referenced somewhere. Since I want a variable inside a C++ class to not be freed, I need to make it global or add it reference to a global object. Since the second option seems better, I create a global IdDict
at the initialization:
jl_value_t* refs = jl_eval_string("refs = IdDict()");
Then, when my variable inside the class is created, I add the reference to this IdDict
by:
jl_value_t* refs = jl_eval_string("refs");
jl_function_t* setindex = jl_get_function(jl_main_module, "setindex!");
...
tle = (jl_value_t*)jl_call2(getindex, (jl_value_t*)array_tle, jl_box_int64(1));
jl_call3(setindex, refs, tle, tle);
In this case, since tle
is referenced in the global variable refs
, it seems that the GC will not delete it.
Seems right?