# Julia GC wiping memory despite use of GC.@preserve

**URL:** <https://discourse.julialang.org/t/julia-gc-wiping-memory-despite-use-of-gc-preserve/57025>\
**Category:** General Usage\
**Tags:** embedding, garbage-collection\
**Created:** [March 12, 2021, 2:15pm UTC](https://discourse.julialang.org/t/julia-gc-wiping-memory-despite-use-of-gc-preserve/57025 "2021-03-12T14:15:45Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![JuliaPerson](https://avatars.discourse-cdn.com/v4/letter/j/a5b964/32.png) [@JuliaPerson](https://discourse.julialang.org/u/JuliaPerson)\
**Post date:** [March 12, 2021, 2:15pm UTC](https://discourse.julialang.org/t/julia-gc-wiping-memory-despite-use-of-gc-preserve/57025/1 "2021-03-12T14:15:45Z")

</div>

Hello fellow Julia users,

I realize that a small working example would be preferred, but I’m working within a fairly large project, and the behavior I’m seeing is not entirely repeatable. I was hoping to get some pointers just based on a description.

Basically I have a custom Julia package. I am trying to interface with this package from a custom C++ library. Basically I have the following call chain heirarchy

1. C++ function calls highest level julia function as follows:

```julia
jl_value_t **gb_args;
JL_GC_PUSHARGS(gb_args, 9);
jl_value_t *input0= jl_box_float64(input0_cpp);
gb_args[0] = intput0;
.
.
gb_args[8] = intput8;

jl_value_t* inputs [9] = {input0, ..., input8}

// Call the function
jl_call(my_jl_function_ptr, inputs,9);
JL_GC_POP();

```

1. my\_jl\_function\_ptr is a pointer to my\_function\_lv1(inputs) inside of julia
2. my\_function\_lv1(inputs) calls my\_function\_lv2(inputs)
3. my\_function\_lv2(inputs) call my\_function\_lv3(inputs)

Now the problem is sometimes everything works just fine, but sometimes one or more of the inputs at any of the levels get wiped. I’ve tried using GC.@preserve on all of the functions, but this doesn’t help since sometimes one of the inputs gets wiped from within the function my\_function\_lv3() before it gets used. The only solution I’ve had success with is within my\_function\_lv1() to do

```julia
GC.enable(false)
my_function_lv2()
GC.enable(true)

```

Is there a better way to do this than completely disabling the garbage collector like this each time I call my\_function\_lv2()?

Thanks!
