C struct garbage collection not run frequently enough

Julia itself already loads libjulia, so that should just work if you’re calling the library from julia.

What I’m saying is that the pointer is the object itself; Julia just abstracts that detail away. Mutable structs in Julia have pointer identity. In that way, Julia already handles the pointer correctly for you and you don’t have to emulate that manually.

You don’t have to do that manually though - the ccall interface handles all of that for you. See here. You’d implement Base.unsafe_convert for RTPSA to give pointer_from_objref and then you can just pass the object as-is back to C.

Julia doesn’t need to know that though - your struct would be exactly the same, pointer fields and all. If you want to inspect the array you can implement some accessing sugar to handle the unsafe_load for you (or use unsafe_wrap with own=false for printing), but other than that, you don’t need to change anything about your struct.

The advantage of this approach lies mainly in the fact that there are no allocations at all anymore. The memory was already allocated by C, there’s no wrapper and since every struct is freed in the same way, the function passed to finalizer can also just be a regular function instead of an anonymous function.

1 Like