# GC extensions segfault

**URL:** <https://discourse.julialang.org/t/gc-extensions-segfault/18517>\
**Category:** General Usage\
**Tags:** question\
**Created:** [December 10, 2018, 1:22pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517 "2018-12-10T13:22:52Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)\
**Post date:** [December 10, 2018, 1:22pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517/1 "2018-12-10T13:22:52Z")

</div>

I wanted to try out the new GC extensions and this is what I’ve got, but it is producing a segfault (as if I were not rooting anything at all).

```julia
#include <julia.h>
#include <julia_gcext.h>

jl_value_t *a, *b, *c;

void root_value(int f)
{
    jl_gc_mark_queue_obj(jl_get_ptls_states(), a);
    jl_gc_mark_queue_obj(jl_get_ptls_states(), b);
    jl_gc_mark_queue_obj(jl_get_ptls_states(), c);
}

int main()
{
	jl_gc_set_cb_root_scanner(root_value, 1);
	a = jl_box_float64(3.14);
	b = jl_box_int64(7);
	c = jl_box_float32(6.28);

	jl_call3(jl_get_function(jl_base_module, "println"), a, b, c);
}

```

(For reproduction: I used

```julia
gcc -o gcext gcext.c -DJULIA_ENABLE_THREADING=1 -I/home/kim/.julia-nightly/include/julia/ -L/home/kim/.julia-nightly/lib/ -ljulia; LD_PRELOAD=/home/kim/.julia-nightly/lib/libjulia.so ./gcext

```

to run this, where `gcext.c` is the test file and `/home/kim/.julia-nightly` is the installation directory of Julia).

What am I doing wrong?

Thanks for the help!

---

<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:** [December 10, 2018, 1:47pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517/2 "2018-12-10T13:47:44Z")

</div>

Where did you get the segfault and why do you think

> as if I were not rooting anything at all

? For a start, you did not initialize julia. Also, `jl_call3` roots the arguments for you so any crash in `jl_call3` won’t be related to GC at all.

---

<div class="post-metadata">

**Author:** ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)\
**Post date:** [December 10, 2018, 3:55pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517/3 "2018-12-10T15:55:58Z")

</div>

Right, I forgot those. But wouldn’t the assignment to b cause the value in a to be released? How could `call3` ever prevent that?

---

<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:** [December 10, 2018, 4:02pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517/4 "2018-12-10T16:02:59Z")

</div>

No `jl_call3` won’t preserve that. I’m just saying that the window for your callback to do anything is really small so whatever crash you see is very likely not related to

> as if I were not rooting anything at all

---

<div class="post-metadata">

**Author:** ![kim366](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kim366/32/5756_2.png) [@kim366](https://discourse.julialang.org/u/kim366)\
**Post date:** [December 10, 2018, 7:10pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517/5 "2018-12-10T19:10:29Z")

</div>

Okay, so the other code did succeed without rooting, but this one does not and setting the callback has no effect.

```julia
#include <julia.h>
#include <julia_gcext.h>
#include <stdio.h>

jl_value_t *fn, *arg1, *arg2;

typedef struct {
  double x, y, z;
} Vec3;

void root_value(int f)
{
  jl_gc_mark_queue_obj(jl_get_ptls_states(), fn);
  jl_gc_mark_queue_obj(jl_get_ptls_states(), arg1);
  jl_gc_mark_queue_obj(jl_get_ptls_states(), arg2);
}

int main() {
  jl_init();
  jl_gc_set_cb_root_scanner(root_value, 1);

  jl_eval_string("using StaticArrays");

  while (1) {
    jl_value_t *res;

    fn = jl_eval_string("+"),
    arg1 = jl_eval_string("MVector(5., 8., 1.2)"),
    arg2 = jl_eval_string("MVector(7., 1., 1.6)");
    res = jl_call2(fn, arg1, arg2);
    Vec3 v = *(Vec3 *)res;
    printf("%f\n", v.z);
  }

  jl_atexit_hook(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:** [December 10, 2018, 7:22pm UTC](https://discourse.julialang.org/t/gc-extensions-segfault/18517/6 "2018-12-10T19:22:39Z")

</div>

And what exactly is the error (output) you get?  
On other issue I can see is that you need to check if the argument is null.
