I tried to nest the GC pairs (JL_GC_PUSH, JL_GC_POP), but got some compile
errors regarding julia.h.
In file included from test6.cpp:11:0:
> test6.cpp: In function ‘int main(int, char**)’:
> /home/xxx/Software/julia-9c76c3e89a/include/julia/julia.h:609:19: error:
> redeclaration of ‘void* __gc_stkf []’
> void *__gc_stkf[] = {(void*)3, jl_pgcstack, arg1};
> \
> ^
> test6.cpp:44:5: note: in expansion of macro ‘JL_GC_PUSH1’
> JL_GC_PUSH1(&ret);
> ^
> /home/xxx/Software/julia-9c76c3e89a/include/julia/julia.h:613:9: note:
> ‘void* __gc_stkf [4]’ previously declared here
> void *__gc_stkf[] = {(void*)5, jl_pgcstack, arg1, arg2};
> \
> ^
> test6.cpp:35:5: note: in expansion of macro ‘JL_GC_PUSH2’
> JL_GC_PUSH2(&a,&b);
So is nesting them not allowed, or is it a bug in julia.h?
My test C++ code is here:
#include <julia.h>
#include <julia_threads.h>
#include <iostream>
using namespace std;
#include <julia.h>
#include <iostream>
using namespace std;
struct TestType {
double a;
double b;
};
int main(int argc, char *argv[])
{
TestType A, *ret;
A.a=2.;
A.b=3.;
jl_init(NULL);
jl_load("TestArg.jl");
jl_value_t * mod = (jl_value_t*)jl_eval_string("TestArg");
jl_value_t* a=NULL;
jl_value_t* b=NULL;
JL_GC_PUSH2(&a,&b);
a = jl_box_float64(A.a);
b = jl_box_float64(A.b);
jl_value_t* jl_A =
jl_new_struct((jl_datatype_t*)jl_get_function((jl_module_t*)mod,
"TestType"), a, b);
// construct a TestType instance
jl_function_t * func = jl_get_function((jl_module_t*)mod,"TestFunc");
ret = (TestType*) jl_call1(func, jl_A);
JL_GC_PUSH1(&ret);
cout<<ret->a<<" "<<ret->b<<endl;
JL_GC_POP();
JL_GC_POP();
jl_atexit_hook(0);
return 0;
}