# Constructors and \`cfunction\`

**URL:** <https://discourse.julialang.org/t/constructors-and-cfunction/9067>\
**Category:** General Usage\
**Created:** [February 14, 2018, 7:58pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067 "2018-02-14T19:58:06Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [February 14, 2018, 7:58pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/1 "2018-02-14T19:58:06Z")

</div>

I just tried to get a C function pointer to a constructor, and was surprised when I got an error:

```julia
julia> struct Thing
       x::Int
       end

julia> cfunction(Thing, Ref{Thing}, (Int,))
ERROR: closures are not yet c-callable
Stacktrace:
 [1] cfunction(::Type{T} where T, ::Type{T} where T, ::Tuple{DataType}) at ./c.jl:24

```

Can someone please explain why that doesn’t work? Do I have an error, or is this expected? Also, what do closures have to do with this?

Interestingly, if I wrap the call to the constructor in another function, I can get a C function pointer to that.

```julia
julia> thing(x) = Thing(x)
thing (generic function with 1 method)

julia> cfunction(thing, Ref{Thing}, (Int,))
Ptr{Void} @0x0000000125f77ef0

```

Thanks in advance for the help!

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [February 15, 2018, 5:11pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/2 "2018-02-15T17:11:52Z")

</div>

A few follow on questions. All of the examples of `cfunction` that I can find return `Cdouble` or `Cvoid`. Assuming I can solve my issues above and call a constructor with a `cfunction`, who is responsible for freeing the memory pointed to by the return value? Is the returned value just a `jl_value_t *` that has been cast to my specified return type and is managed by the Julia garbage collector?

---

<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:** [February 15, 2018, 5:35pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/3 "2018-02-15T17:35:52Z")

</div>

For nonleaf return type the return value is a pointer. There’s no cast since it’s a c specific concept. The GC is responsible for freezing it and you are responsible for rooting it so that GC doesn’t do it too early.

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [February 15, 2018, 5:57pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/4 "2018-02-15T17:57:53Z")

</div>

So can I cast the return type to a `jl_value_t *`? I actually need to keep the value around, and so I think I can’t just use `JL_GC_PUSH1` on it. Instead, I was thinking of doing:

```julia
jl_set_const(jl_main_module, jl_gensym(), (jl_value_t *)the_object);

```

---

<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:** [February 15, 2018, 6:20pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/5 "2018-02-15T18:20:30Z")

</div>

Julia doesn’t care how you use it in c so any c cast on it is fine. Setting it as a module field does not guarantee pointer validity for immutable types. You can use a `Vector{Any}` though, which should also be significantly simpler for cleanup.

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [February 15, 2018, 7:50pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/6 "2018-02-15T19:50:48Z")

</div>

Thanks for the help! Do you have any comments on what is going on in my original question, where I try to call `cfunction` on a constructor?

---

<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:** [February 15, 2018, 9:13pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/7 "2018-02-15T21:13:55Z")

</div>

Sounds like a bug.

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [February 15, 2018, 9:26pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/8 "2018-02-15T21:26:48Z")

</div>

OK, thanks, I’ll open an issue.

---

<div class="post-metadata">

**Author:** ![danielmatz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/danielmatz/32/2285_2.png) [@danielmatz](https://discourse.julialang.org/u/danielmatz)\
**Post date:** [February 15, 2018, 9:37pm UTC](https://discourse.julialang.org/t/constructors-and-cfunction/9067/9 "2018-02-15T21:37:14Z")

</div>

[https://github.com/JuliaLang/julia/issues/26070](https://github.com/JuliaLang/julia/issues/26070)
