# How to use \`GC\_PUSH\` macros

**URL:** <https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304>\
**Category:** New to Julia\
**Created:** [August 30, 2018, 11:09am UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304 "2018-08-30T11:09:23Z")\
**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:** [August 30, 2018, 11:09am UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304/1 "2018-08-30T11:09:23Z")

</div>

The following code segfaults consistently after ~2.5M allocations. I previously had not `GC_PUSH` macros in place and didn’t save `jl_value_t` pointers as stack variables. I was advised to root the values, which is a term I do not quite understand. I added the garbage collector macros and nothing changed, I still get the same segmentation fault. Te segmentation fault does not occur if the while loop is commented out

What am I doing wrong/

```nohighlight
#include <julia/julia.h>

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

int main() {
  jl_init();

  jl_eval_string("using StaticArrays");

  while (1) {
    jl_value_t *res;

    {
      jl_value_t *fn = jl_eval_string("+"),
                 *arg1 = jl_eval_string("MVector(5., 8., 1.2)"),
                 *arg2 = jl_eval_string("MVector(7., 1., 1.6)");
      JL_GC_PUSH3(&fn, &arg1, &arg2);
      res = jl_call2(fn, arg1, arg2);
      JL_GC_POP();
    }
    JL_GC_PUSH1(&res);
    Vec3 v = *(Vec3 *)res;
    JL_GC_POP();
  }

  jl_atexit_hook(0);
}

```

---

<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:** [August 30, 2018, 12:20pm UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304/2 "2018-08-30T12:20:00Z")

</div>

I may be wrong, but I suspect that your GC protection comes too late; the value pointed to by `fn` might get garbage collected while you do `jl_eval_string` for `arg1`, and so on. Try

```julia
    {
      jl_value_t *fn = 0;
      jl_value_t *arg1 = 0;
      jl_value_t *arg2 = 0;
      JL_GC_PUSH3(&fn, &arg1, &arg2);
      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);
      JL_GC_POP();
    }

```

---

<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:** [August 30, 2018, 1:02pm UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304/3 "2018-08-30T13:02:49Z")

</div>

Yup, that seems to work! That means that the documentation is incorrect, as it shows

```nohighlight
jl_value_t *ret = jl_eval_string("sqrt(2.0)");
JL_GC_PUSH1(&ret);
// Do something with ret
JL_GC_POP();

```

---

<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:** [August 30, 2018, 1:08pm UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304/4 "2018-08-30T13:08:08Z")

</div>

No, that case is fine. It’s not until you call into Julia again that garbage collection may happen. The problem comes when you compute your second `jl_value_t` without having protected the first value.

---

<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:** [August 30, 2018, 1:09pm UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304/5 "2018-08-30T13:09:39Z")

</div>

I see. I will keep that in mind. Thank you!

---

<div class="post-metadata">

**Author:** ![Keno](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/keno/32/285_2.png) [@Keno](https://discourse.julialang.org/u/Keno)\
**Post date:** [August 30, 2018, 1:50pm UTC](https://discourse.julialang.org/t/how-to-use-gc-push-macros/14304/6 "2018-08-30T13:50:18Z")

</div>

Please submit a documentation PR if you think it could have been clearer and helped prevent your confusion.
