Segmentation fault in embedded Julia

Dear Julia community,

I am developing a Windows desktop application in C++ that uses functionality developed in Julia (v0.3). The application accesses calls Julia indirectly through a DLL, the implementation of which follows the indications of [Embedding Julia · The Julia Language].

I have noticed that in long tasks (requiring several calls to Julia), the application ends up crashing. Debugging of the C++ application shows that:

  • The crash does not always occur at the same stage of the computation (sometimes earlier, sometimes later, but if the task is large enough, it ends up happening).
  • It is always a segmentation fault
  • The call stack is, from top to bottom:
    • libjulia.dll!push_root()
    • libjulia.dll!gc_mark_task(…)
    • libjulia.dll!_gc_collect()
    • libjulia.dll!allow_2w()
    • libjulia.dll!jl_box_float_64()
  • Below in the call stack, there is always a function of type:
void some_function(void* object, double x, double y){
    jl_value_t** args;
    JL_GC_PUSHARGS(args,3);
    args[0] = (jl_value_t*)object;
    args[1] = jl_box_float64(x);
    args[2] = jl_box_float64(y);
    jl_call(ref_to_jl_function, args, 3);
    JL_GC_POP();
}

I assume that there is something wrong in the way I use the Garbage Collector functions and Macros but, unfortunately, my knowledge is insufficient to assess what could be wrong. I would therefore appreciate some help as to what might be happening.

Best regards

Then you didn’t use:

that supports Julia 1.7.0+.

I would recommend using the supported latest Julia (maybe even check 1.9-DEV), 1.8.2 should be out on the download page soon (it’s already available in source code). I don’t know that any older wouldn’t work for you with or without jluna.

The embedding API should of course work from C (and then C++), but you might likely be doing something wrong. I can’t help debug, just know jluna should be much preferred, if you embed into C++ rather than C.

1 Like

That’s just way too out of date. Why are you using a Julia version from pre 2012?

Thank you for the replies. The code was developed for v0.3. Even if I upgrade it (which is a very big task), I still would like to know what is wrong with my implementation, because it seems that I have followed the instructions for embedding Julia in C.

It seems most likely there’s a bug in that ancient version of Julia.

How big is the code?

There’s nothing inherently wrong with using Julia 0.3… if it works (likely not good advice though). But it doesn’t. I recall there are even automated tools to upgrade between some older versions (and there’s Compat.jl). I tried to google upgrade information, since it’s been a while, and I found, not helpful (I just found funny):

1 Like

Not answering the question, but maybe it’s worth trying calling a standalone Julia program via interprocess communication, rather than embedding it into C++.

1 Like

Possible, no doubt, but I wouldn’t count it as highly likely. Getting the GC interactions correct in the embedding API has always been very tricky, to the point that I strongly recommend doing all interaction between embedding program and Julia code via cfunction pointers. I don’t remember whether this option was available in Julia 0.3, which is not pre 2012 by the way, however.

2 Likes

My mistake, it was 2014 release.

That is worth considering as an alternative solution. I fear that the performance would be lower. Could you provide a link to some example of how can this be effectively achieved?