# Access fields of pointer to struct returned from ccall

**URL:** <https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985>\
**Category:** General Usage\
**Tags:** ccall\
**Created:** [July 30, 2019, 3:44pm UTC](https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985 "2019-07-30T15:44:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [July 30, 2019, 3:44pm UTC](https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985/1 "2019-07-30T15:44:56Z")

</div>

I am calling a C function that returns a pointer to a struct.  
I have mapped that struct to a corresponding Julia type (see code below), and when I call the function I get a `Ptr{T}`.  
How can access the fields of the returned pointer?

```julia
mutable struct XXH32_state_t
   total_len_32::UInt32
   large_len::UInt32
   v1::UInt32
   v2::UInt32
   v3::UInt32
   v4::UInt32
   mem32::NTuple{4,UInt32}
   memsize::UInt32
   reserved::UInt32
end

@inline function XXH32_createState()::Ref{XXH32_state_t}
   sr = ccall((:XXH32_createState, libxxhash), Ptr{XXH32_state_t}, ())
end

```

calling the function

```julia
sr=XXH32_createState()

```

returns:

```julia
Ptr{XXH32_state_t} @0x00000000014fc4f0

```

---

<div class="post-metadata">

**Author:** ![foobar\_lv2](https://avatars.discourse-cdn.com/v4/letter/f/ee59a6/32.png) [@foobar\_lv2](https://discourse.julialang.org/u/foobar_lv2)\
**Post date:** [July 30, 2019, 6:55pm UTC](https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985/2 "2019-07-30T18:55:21Z")

</div>

This is somewhat annoying. For read-only access, you can simply `unsafe_load(sr)` and obtain a new `XXH32_state_t`, the fields of which are copies of `sr`.

Generally, this is not what you want. The problem is that `mutable struct` implies that the object is managed by the julia garbage collector and preceded by a julia-specific object header. Your C library allocates the blob of memory, and it is therefore impossible to wrap it into a julia object of this type.

The proper type would be `struct XXH32_state_t ... end`. Then you could define a `mutable struct XXH32_state_handle ptr::Ptr{XXH32_state_t} end` that exposes all the relevant `getproperty` / `setproperty!`, and optionally has a finalizer that de-allocates the object (either by calling `free` if the object was simply `malloc`ed, or by calling something like `XXH32_freeState`).

The package [blobs.jl](https://github.com/RelationalAI-oss/Blobs.jl) is fantastic for exploring more ergonomic C interop. This [PR](https://github.com/JuliaLang/julia/pull/21912) is also relevant.

If you only need a single type, then Blobs.jl is probably not worth the dependency, though (one page of boilerplate code is easier to audit than a dependency on a rarely used package).

---

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [July 30, 2019, 11:47pm UTC](https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985/3 "2019-07-30T23:47:25Z")

</div>

Thanks for your advice  
You guessed correctly that the library has a `XXH32_freeState` function, and I would like to use your idea regarding a `finalizer`.  
The functions are used to hash a data stream, thus I named the immutable struct:

```julia
mutable struct XXH32stream
   state_ptr::Ptr{XXH32_state_t}
   function XXH32stream()
      sp = XXH32_createState()
      new(sp)
      finalizer(state_ptr, XXH32_freeState)
   end
end

```

The finalizer statement raises the error that `state_ptr` is not defined.  
Is there a way to reference the struct’s fields from within an inner constructor?  
If not, what is the “proper” way to register a finalizer when creating an object?

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [July 30, 2019, 11:50pm UTC](https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985/4 "2019-07-30T23:50:58Z")

</div>

```julia
...
     stream = new(sp)
     finalizer(stream.state_ptr, XXH32_freeState)
     return stream
...

```

perhaps?

But the function should be the first argument.

---

<div class="post-metadata">

**Author:** ![hros](https://avatars.discourse-cdn.com/v4/letter/h/97f17d/32.png) [@hros](https://discourse.julialang.org/u/hros)\
**Post date:** [July 31, 2019, 12:20am UTC](https://discourse.julialang.org/t/access-fields-of-pointer-to-struct-returned-from-ccall/26985/5 "2019-07-31T00:20:31Z")

</div>

Thanks  
I had to make a slight modification, as the proposed syntax raised the error:

```julia
ERROR: objects of type Ptr{XXH32_state_t} cannot be finalized

```

Which “agrees” with the documentation: `The type of x must be a mutable struct, otherwise the behavior of this function is unpredictable.`  
Thus, I ended up with the following, which seems to work:

```julia
mutable struct XXH32stream
   state_ptr::Ptr{XXH32_state_t}
   function XXH32stream()
      sp = XXH32_createState()
      stream = new(sp)
      finalizer(x->XXH32_freeState(x.state_ptr), stream)
      return stream
   end
end

```
