# Calling JL\_GC\_PUSH1 multiple times

**URL:** <https://discourse.julialang.org/t/calling-jl-gc-push1-multiple-times/18666>\
**Category:** General Usage\
**Created:** [December 14, 2018, 4:49pm UTC](https://discourse.julialang.org/t/calling-jl-gc-push1-multiple-times/18666 "2018-12-14T16:49:59Z")\
**Posts on this page:** 1\
**Showing post:** 3

<div class="post-metadata">

**Author:** ![Ronis\_BR](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ronis_br/32/50999_2.png) [@Ronis\_BR](https://discourse.julialang.org/u/Ronis_BR)\
**Post date:** [December 14, 2018, 5:48pm UTC](https://discourse.julialang.org/t/calling-jl-gc-push1-multiple-times/18666/3 "2018-12-14T17:48:44Z")

</div>

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 create a global `IdDict` at the initialization:

```nohighlight
    jl_value_t* refs = jl_eval_string("refs = IdDict()");

```

Then, when my variable inside the class is created, I add the reference to this `IdDict` by:

```nohighlight
    jl_value_t* refs = jl_eval_string("refs");
    jl_function_t* setindex = jl_get_function(jl_main_module, "setindex!");

    ...

    tle = (jl_value_t*)jl_call2(getindex, (jl_value_t*)array_tle, jl_box_int64(1));

    jl_call3(setindex, refs, tle, tle);

```

In this case, since `tle` is referenced in the global variable `refs`, it seems that the GC will not delete it.

Seems right?

---

_[View the full topic](https://discourse.julialang.org/t/calling-jl-gc-push1-multiple-times/18666)._
