# Segmentation fault in embedded Julia

**URL:** <https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917>\
**Category:** General Usage\
**Created:** [September 28, 2022, 10:42am UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917 "2022-09-28T10:42:01Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![cariocecus](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cariocecus](https://discourse.julialang.org/u/cariocecus)\
**Post date:** [September 28, 2022, 10:42am UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/1 "2022-09-28T10:42:01Z")

</div>

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](https://docs.julialang.org/en/v1/manual/embedding/)].

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:

```julia
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

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 28, 2022, 10:57am UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/2 "2022-09-28T10:57:59Z")

</div>

> [@cariocecus](#):
>
> in C++ that uses functionality developed in Julia (v0.3).

Then you didn’t use:

> **[GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety,...](https://github.com/Clemapfel/jluna)**
>
> Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of Use - GitHub - Clemapfel/jluna: Julia Wrapper for C++ with Focus on Safety, Elegance, and Ease of 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.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 29, 2022, 4:04am UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/3 "2022-09-29T04:04:25Z")

</div>

> [@cariocecus](#):
>
> Julia (v0.3)

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

---

<div class="post-metadata">

**Author:** ![cariocecus](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cariocecus](https://discourse.julialang.org/u/cariocecus)\
**Post date:** [September 29, 2022, 10:02am UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/4 "2022-09-29T10:02:28Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 29, 2022, 1:54pm UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/5 "2022-09-29T13:54:07Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)\
**Post date:** [September 29, 2022, 2:08pm UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/6 "2022-09-29T14:08:58Z")

</div>

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):

> **[Upgrade to Julia Version 0.3.0](https://juliaeconomics.com/2014/09/01/upgrade-to-julia-version-3/)**
>
> UPDATE: Julia is now on Version 0.4 and the Julia Developers have now released an IDE called Juno that is bundled with Julia. See the new installation guide here.   The Julia developers have o…

---

<div class="post-metadata">

**Author:** ![greatpet](https://avatars.discourse-cdn.com/v4/letter/g/e495f1/32.png) [@greatpet](https://discourse.julialang.org/u/greatpet)\
**Post date:** [September 29, 2022, 3:19pm UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/7 "2022-09-29T15:19:57Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [September 29, 2022, 5:46pm UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/8 "2022-09-29T17:46:06Z")

</div>

> [@dlakelan](#):
>
> It seems most likely there’s a bug in that ancient version of Julia.

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](https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl/blob/master/docs/theory.md#creating-and-retrieving-cfunction-pointers) 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.

---

<div class="post-metadata">

**Author:** ![dlakelan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlakelan/32/8491_2.png) [@dlakelan](https://discourse.julialang.org/u/dlakelan)\
**Post date:** [September 29, 2022, 8:04pm UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/9 "2022-09-29T20:04:08Z")

</div>

My mistake, it was 2014 release.

---

<div class="post-metadata">

**Author:** ![cariocecus](https://avatars.discourse-cdn.com/v4/letter/c/3d9bf3/32.png) [@cariocecus](https://discourse.julialang.org/u/cariocecus)\
**Post date:** [September 30, 2022, 6:27am UTC](https://discourse.julialang.org/t/segmentation-fault-in-embedded-julia/87917/10 "2022-09-30T06:27:16Z")

</div>

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?
