# Support with embedding julia in C++

**URL:** <https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054>\
**Category:** General Usage\
**Created:** [July 25, 2017, 11:34am UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054 "2017-07-25T11:34:44Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![yogi](https://avatars.discourse-cdn.com/v4/letter/y/46a35a/32.png) [@yogi](https://discourse.julialang.org/u/yogi)\
**Post date:** [July 25, 2017, 11:34am UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/1 "2017-07-25T11:34:44Z")

</div>

We are embedding julia in C++. The first few calls to the function are completed successfully, but after a few times the following error comes up.

while loading no file, in expression starting on line 389  
jl\_subtype\_le at /home/centos/buildbot/slave/package\_tarball32/build/src/jltypes.c:2746  
jl\_f\_isa at /home/centos/buildbot/slave/package\_tarball32/build/src/builtins.c:396  
isvarargtype at ./essentials.jl:63 [inlined]  
Type at ./inference.jl:122  
unknown function (ip: 0xf0c501f6)  
jl\_call\_method\_internal at /home/centos/buildbot/slave/package\_tarball32/build/src/julia\_internal.h:211 [inlined]  
jl\_apply\_generic at /home/centos/buildbot/slave/package\_tarball32/build/src/gf.c:1950  
typeinf\_edge at ./inference.jl:1577  
jl\_call\_method\_internal at /home/centos/buildbot/slave/package\_tarball32/build/src/julia\_internal.h:211 [inlined]  
jl\_apply\_generic at /home/centos/buildbot/slave/package\_tarball32/build/src/gf.c:1950  
typeinf\_ext at ./inference.jl:1621  
jl\_call\_method\_internal at /home/centos/buildbot/slave/package\_tarball32/build/src/julia\_internal.h:211 [inlined]  
jl\_apply\_generic at /home/centos/buildbot/slave/package\_tarball32/build/src/gf.c:1950  
jl\_apply at /home/centos/buildbot/slave/package\_tarball32/build/src/julia.h:1388 [inlined]  
jl\_type\_infer at /home/centos/buildbot/slave/package\_tarball32/build/src/gf.c:213  
jl\_compile\_for\_dispatch at /home/centos/buildbot/slave/package\_tarball32/build/src/gf.c:1298  
jl\_call\_method\_internal at /home/centos/buildbot/slave/package\_tarball32/build/src/julia\_internal.h:206 [inlined]  
jl\_apply\_generic at /home/centos/buildbot/slave/package\_tarball32/build/src/gf.c:1950  
jl\_apply at /home/centos/buildbot/slave/package\_tarball32/build/src/julia.h:1388 [inlined]  
jl\_call3 at /home/centos/buildbot/slave/package\_tarball32/build/src/jlapi.c:196  
unknown function (ip: 0x8059bb6)  
unknown function (ip: 0x805024f)  
unknown function (ip: 0x805383e)  
unknown function (ip: 0x805bd1c)  
unknown function (ip: 0xf73dd37c)  
start\_thread at /build/eglibc-0jBZkF/eglibc-2.19/nptl/pthread\_create.c:312  
unknown function (ip: 0xf723b3ed)  
Allocations: 75763006 (Pool: 75761422; Big: 1584); GC: 27  
Segmentation fault (core dumped)

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [July 25, 2017, 12:36pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/2 "2017-07-25T12:36:15Z")

</div>

I think it would be helpful if you could show what function you are calling and how you are calling it.

---

<div class="post-metadata">

**Author:** ![yogi](https://avatars.discourse-cdn.com/v4/letter/y/46a35a/32.png) [@yogi](https://discourse.julialang.org/u/yogi)\
**Post date:** [July 25, 2017, 3:20pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/3 "2017-07-25T15:20:37Z")

</div>

We are working with 32 bit Julia 0.5.2

Below is the structure of the C++ and Julia code.

// Code in c++

```
    jl_function_t *func1 = jl_get_function(jl_current_module, "func1");
    jl_function_t *func2 = jl_get_function(jl_current_module, "func2");

    jl_value_t* float_array_type = jl_apply_array_type(jl_float32_type, 1);
    jl_value_t* int_array_type = jl_apply_array_type(jl_int32_type, 1);
    jl_value_t* char_array_type = jl_apply_array_type(jl_int8_type, 1);

    jl_array_t* arr1_jl = jl_ptr_to_array_1d( float_array_type, arr1, max_depth+1, 0); // all arrays have size max_depth+1
    jl_array_t* arr2_jl = jl_ptr_to_array_1d( float_array_type, arr2, max_depth+1, 0);

    jl_array_t* bool_arr1_jl = jl_ptr_to_array_1d( int_array_type, bool_arr1, max_depth+1, 0);
    jl_array_t* bool_arr2_jl = jl_ptr_to_array_1d( int_array_type, bool_arr2, max_depth+1, 0);

    symbol_arr_jl = jl_ptr_to_array_1d( char_array_type, symbol, symbol_size, 0);

    jl_call3(func1, (jl_value_t*)symbol_arr_jl, (jl_value_t*)arr1_jl, (jl_value_t*)bool_arr1_jl);
    jl_call3(func2, (jl_value_t*)symbol_arr_jl, (jl_value_t*)arr2_jl, (jl_value_t*)bool_arr2_jl);

```

// Julia code  
function func1( symbol, input\_arr, bool\_res\_arr)  
symbol\_str = map(symbol, Char)  
…  
end

function func2( symbol, input\_arr, bool\_res\_arr)  
symbol\_str = map(symbol, Char)  
…  
end

We are performing basic operations on input\_arr and return the result by modifying bool\_res\_arr. The c++ code is executed multiple times, and behaves as expected (returns correct result in bool\_res\_arr) but after a random number of runs crashes with the error posted above. If we add calls to gc before this snippet, the chances of error reduce.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 25, 2017, 3:31pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/4 "2017-07-25T15:31:38Z")

</div>

Not sure if that’s the reason but you are missing all of the GC roots. All of the arrays need to be rooted.

---

<div class="post-metadata">

**Author:** ![yogi](https://avatars.discourse-cdn.com/v4/letter/y/46a35a/32.png) [@yogi](https://discourse.julialang.org/u/yogi)\
**Post date:** [July 25, 2017, 3:39pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/5 "2017-07-25T15:39:04Z")

</div>

I am not familiar by the notion of GC roots and the embedding julia docs page does not mention rooting. As the arrays are owned and managed in c++, I have passes last argument in jl\_ptr\_to\_array\_1d as 0.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 25, 2017, 3:47pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/6 "2017-07-25T15:47:32Z")

</div>

> [@yogi](#):
>
> the embedding julia docs page does not mention rooting

[https://docs.julialang.org/en/latest/manual/embedding/#Memory-Management-1](https://docs.julialang.org/en/latest/manual/embedding/#Memory-Management-1)

---

<div class="post-metadata">

**Author:** ![yogi](https://avatars.discourse-cdn.com/v4/letter/y/46a35a/32.png) [@yogi](https://discourse.julialang.org/u/yogi)\
**Post date:** [July 25, 2017, 4:10pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/7 "2017-07-25T16:10:51Z")

</div>

Thank you, I had read the Memory management section but was unaware it was also referred to GC rooting. I had assumed that for c++ managed arrays I do not need to push( and pop appropriately) jl\_value\_t objects, but from your comments it appears that this is not the case. Also, say that the array types(or jl function pointers) are initialized once and used at multiple times in other sections of code, then should they also be pushed onto the GC stack.

---

<div class="post-metadata">

**Author:** ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)\
**Post date:** [July 25, 2017, 4:13pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/8 "2017-07-25T16:13:40Z")

</div>

Leaftypes and constant globals don’t need to be rooted…

> [@yogi](#):
>
> was unaware it was also referred to GC rooting

That’s basically the first/only thing it talks about? PRs to make this more clear is welcome.

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [July 25, 2017, 8:47pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/9 "2017-07-25T20:47:36Z")

</div>

This kind of thing is somewhat simpler if [CxxWrap.jl](https://github.com/JuliaInterop/CxxWrap.jl) (or at least the jlcxx C++ part) is an acceptable dependency:

```nohighlight
jlcxx::JuliaFunction func1("func1");
float arr1_jl[] = {1.0, 2.0, 3.0};
func1((jl_value_t*)jlcxx::ArrayRef<float, 1>(&arr1_jl[0], 3).wrapped());

```

The `ArrayRef` interface could still use some polishing, though, including auto-conversion when using in a `JuliaFunction`.

---

<div class="post-metadata">

**Author:** ![yogi](https://avatars.discourse-cdn.com/v4/letter/y/46a35a/32.png) [@yogi](https://discourse.julialang.org/u/yogi)\
**Post date:** [July 26, 2017, 6:36am UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/10 "2017-07-26T06:36:28Z")

</div>

Thanks, I was unaware CxxWrap could be used for calling julia functions from C++. My issue was resolved when I started pushing the jl\_value\_t objects and popping them immediately after the julia call. While this works I also tried initializing the jl\_value\_t objects once, pushing them onto the stack and then reusing them for every call to julia. This approach however fails and crashes spectacularly. Could you explain why this fails?

---

<div class="post-metadata">

**Author:** ![barche](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/barche/32/79_2.png) [@barche](https://discourse.julialang.org/u/barche)\
**Post date:** [July 26, 2017, 7:20am UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/11 "2017-07-26T07:20:40Z")

</div>

From what I understand, the wrapper around the array is allocated and can be garbage collected. I believe the allocation happens here:  
[https://github.com/JuliaLang/julia/blob/release-0.6/src/array.c#L281](https://github.com/JuliaLang/julia/blob/release-0.6/src/array.c#L281)

To prevent this, the recommended method is to store a global reference to the object in an array. CxxWrap provides the `(un)protect_from_gc` function for this, or you could fairly easily roll out your own version, here is the code for reference:  
[https://github.com/JuliaInterop/CxxWrap.jl/blob/master/deps/src/jlcxx/include/jlcxx/type\_conversion.hpp#L49-L99](https://github.com/JuliaInterop/CxxWrap.jl/blob/master/deps/src/jlcxx/include/jlcxx/type_conversion.hpp#L49-L99)

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [July 26, 2017, 3:19pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/12 "2017-07-26T15:19:22Z")

</div>

> [@yuyichao](#):
>
> That’s basically the first/only thing it talks about? PRs to make this more clear is welcome.

Although it discusses the underlying issue, that chapter of the manual doesn’t actually use the term “root” at all with regard to GC. It would probably be helpful if the manual mentioned this technical terminology somewhere, so that people can understand Julia devs when they use the word.

---

<div class="post-metadata">

**Author:** ![Amin\_Yahyaabadi](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amin_yahyaabadi/32/9826_2.png) [@Amin\_Yahyaabadi](https://discourse.julialang.org/u/Amin_Yahyaabadi)\
**Post date:** [August 4, 2019, 10:46pm UTC](https://discourse.julialang.org/t/support-with-embedding-julia-in-c/5054/13 "2019-08-04T22:46:02Z")

</div>

If anyone is reading this later, go to these to links:

Calling Julia functions from C++:

> **[GitHub - JuliaInterop/CxxWrap.jl: Package to make C++ libraries available in...](https://github.com/JuliaInterop/CxxWrap.jl#calling-julia-functions-from-c)**
>
> Package to make C++ libraries available in Julia. Contribute to JuliaInterop/CxxWrap.jl development by creating an account on GitHub.

and  
[Embedding Julia](https://docs.julialang.org/en/v1/manual/embedding/index.html#Embedding-Julia-1)  
Here is the example on GitHub:  
[embedding example](https://github.com/JuliaLang/julia/tree/master/test/embedding)

and these threads:

> [@Calling my own Julia module from C](https://discourse.julialang.org/t/calling-my-own-julia-module-from-c/23779/2):
>
> I don’t have too much experience with this but I’ve gotten it to work. I found [this thread](https://discourse.julialang.org/t/calling-jl-gc-push1-multiple-times/18666) very helpful. The thread is about dealing with garbage collection when calling julia from C but the first post has a good example of loading a module from C (C++ actually but I think everything there works in C) and then calling a function from that module. I’ve used that approach to call my own modules from C. As far as I can tell loading modules is not in the embedding documentation though so I’m not su…

> [@Calling JL\_GC\_PUSH1 multiple times](https://discourse.julialang.org/t/calling-jl-gc-push1-multiple-times/18666/3):
>
> 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 …
