# Returning arbitrary Julia value from C function

**URL:** <https://discourse.julialang.org/t/returning-arbitrary-julia-value-from-c-function/11429>\
**Category:** General Usage\
**Created:** [June 5, 2018, 4:17am UTC](https://discourse.julialang.org/t/returning-arbitrary-julia-value-from-c-function/11429 "2018-06-05T04:17:45Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 5, 2018, 4:17am UTC](https://discourse.julialang.org/t/returning-arbitrary-julia-value-from-c-function/11429/1 "2018-06-05T04:17:45Z")

</div>

I have a general question. Suppose I have a C function which creates and returns an arbitrary Julia value (e.g. an Array) as below:

```julia
jl_value_t* sample_func() {
  jl_value_t* value = nullptr;
  JL_GC_PUSH!(&value);
  // code to create some Julia object and assign it to value
  // ...
  JL_GC_POP();
  return value;
}

```

My question is, if I call the above function from julia using `ccall`, is there any guarantee that after `JL_GC_POP()` and before `value`’s variable is assigned to a Julia variable this object won’t be garbage collected? If not, what’s the safe way to return arbitrary Julia values from a C function?

---

<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:** [June 5, 2018, 11:47am UTC](https://discourse.julialang.org/t/returning-arbitrary-julia-value-from-c-function/11429/2 "2018-06-05T11:47:27Z")

</div>

Yes as long as you call it with `Any` or `Ref{<actual type>}` return type in `ccall`.

---

<div class="post-metadata">

**Author:** ![jinliangwei](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jinliangwei/32/1929_2.png) [@jinliangwei](https://discourse.julialang.org/u/jinliangwei)\
**Post date:** [June 5, 2018, 2:39pm UTC](https://discourse.julialang.org/t/returning-arbitrary-julia-value-from-c-function/11429/3 "2018-06-05T14:39:07Z")

</div>

Thank you!
