# GC error / sefgault in a simple C wrapper

**URL:** <https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032>\
**Category:** General Usage\
**Tags:** ccall, garbage-collection, c, segfault\
**Created:** [March 17, 2025, 12:37am UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032 "2025-03-17T00:37:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [March 17, 2025, 12:37am UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032/1 "2025-03-17T00:37:26Z")

</div>

I am attempting to make a julia wrapper for a C library. The C library is already available as a jll. I was attempting to use Clang.jl to make the wrapper, but whatever I am doing seems to be causing segfaults on garbage collection.

Here is what Clang.jl generates for me (only the minimal amount of bindings to show the problem) and the segfault it causes:

I am wrapping the graph theory library `igraph`, in particular I am creating an empty struct `igraph_t` representing a graph, then initializing it to be the “4 nodes and 0 edges” graph by using `igraph_empty`.

```julia
# bindings generated by Clang.jl and selectively copied here by me
using igraph_jll

using CEnum

@cenum igraph_error_type_t::UInt32 begin
    IGRAPH_SUCCESS = 0
    IGRAPH_FAILURE = 1
    # ... skip a bunch of other options
end

const igraph_error_t = igraph_error_type_t

const igraph_integer_t = Int64

const igraph_bool_t = Bool

mutable struct igraph_i_property_cache_t end

mutable struct igraph_vector_int_t
    stor_begin::Ptr{igraph_integer_t}
    stor_end::Ptr{igraph_integer_t}
    _end::Ptr{igraph_integer_t}
    igraph_vector_int_t() = new()
end

mutable struct igraph_s
    n::igraph_integer_t
    directed::igraph_bool_t
    from::igraph_vector_int_t
    to::igraph_vector_int_t
    oi::igraph_vector_int_t
    ii::igraph_vector_int_t
    os::igraph_vector_int_t
    is::igraph_vector_int_t
    attr::Ptr{Cvoid}
    cache::Ptr{igraph_i_property_cache_t}
    igraph_s() = new()
end

const igraph_t = igraph_s

function igraph_empty(graph, n, directed)
    ccall((:igraph_empty, libigraph), igraph_error_t, (Ptr{igraph_t}, igraph_integer_t, igraph_bool_t), graph, n, directed)
end

##
# simple usage example that segfaults

a = igraph_s();
igraph_empty(pointer_from_objref(a), 4, false)
GC.gc() # segfaults

```

Any idea where I am going wrong? Introspecting some of the properties of `a` like `a.n` and `a.directed` gives me the expected values, but looking at `a.to` which should be storing the destination for each edge causes a segfault.

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [March 17, 2025, 12:17pm UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032/2 "2025-03-17T12:17:15Z")

</div>

> mutable struct igraph\_vector\_int\_t  
> stor\_begin::Ptr{igraph\_integer\_t}  
> stor\_end::Ptr{igraph\_integer\_t}  
> \_end::Ptr{igraph\_integer\_t}  
> igraph\_vector\_int\_t() = new()  
> end

this should be

```julia
struct igraph_vector_int_t
    stor_begin::Ptr{igraph_integer_t}
    stor_end::Ptr{igraph_integer_t}
    _end::Ptr{igraph_integer_t}
end

```

---

<div class="post-metadata">

**Author:** ![Gnimuc](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gnimuc/32/2194_2.png) [@Gnimuc](https://discourse.julialang.org/u/Gnimuc)\
**Post date:** [March 17, 2025, 12:18pm UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032/3 "2025-03-17T12:18:31Z")

</div>

I don’t think Clang.jl will generate non-top-level structs as mutable. Did you misuse some configuration options?

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [March 17, 2025, 1:15pm UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032/4 "2025-03-17T13:15:05Z")

</div>

Thanks! That is indeed my fault. Could you help me understand better what is happening though (I thought I need these changes for the following reasons)?

I thought only mutable structs can be used with `pointer_from_objref` which itself is necessary to interface between julia and c functions acting on objects managed by julia. In particular, I want to be able to work with `igraph_vector_int_t` directly.

For instance, `igraph` has functions that use explicitly instances of both of these structs as input arguments: if I want to be able to wrap `igraph_somefunction(::Ptr{igraph_t}, ::Ptr{igraph_vector_int_t})` I need a mutable `igraph_vector_int_t`, right? Is there a correct way to do that?

---

<div class="post-metadata">

**Author:** ![fatteneder](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fatteneder/32/33991_2.png) [@fatteneder](https://discourse.julialang.org/u/fatteneder)\
**Post date:** [March 17, 2025, 1:26pm UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032/5 "2025-03-17T13:26:40Z")

</div>

> [@Krastanov](#):
>
> Could you help me understand better what is happening though

The `igraph_t` type wants the fields of type `igraph_vector_int_t` to be stored inline (cf. [igraph/include/igraph\_datatype.h at master · igraph/igraph · GitHub](https://github.com/igraph/igraph/blob/master/include/igraph_datatype.h#L110-L121)),  
but declaring the julia type to be `mutable` prevents that. (Check the `fieldoffset`s of `igraph_s` and compare with `sizeof(igraph_vector_int_t)`).

> [@Krastanov](#):
>
> Is there a correct way to do that?

Use `Ref(...)` instead of `pointer_from_objref`.

---

<div class="post-metadata">

**Author:** ![Krastanov](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krastanov/32/6817_2.png) [@Krastanov](https://discourse.julialang.org/u/Krastanov)\
**Post date:** [March 17, 2025, 1:27pm UTC](https://discourse.julialang.org/t/gc-error-sefgault-in-a-simple-c-wrapper/127032/6 "2025-03-17T13:27:39Z")

</div>

Thank you both, this has been very helpful!
