Memory Errors at Julia-C interface and Safe Pointer Usage

I’ve discovered and fixed one issue: my strategy of using a Cshort to emulate a C struct of packed bitfields was messing with field alignment. Changing that to a Cuint makes it usually run successfully, but it still gives memory errors every now and then. Checking the offsets of the struct fields (as in the help entry for fieldoffset), they now all match between the C and the Julia, so that’s not the problem.

Useful things learned while debugging: ccall(:jl_, Cvoid, (Any,), x) is a nice way to get low-level info about x. Julia’s -g debug flag made the memory error more consistent. lldb is a neat tool, but not very useful in this case (no debug symbols in the compiled C library).

I’ve updated my minimum working example to incorporate the above comments and a few other things: eg to compare apples to apples, I ought to heap allocate the arrays in the C. You may need to run the Julia file a couple times in order to get the memory error (-g flag recommended).

mwe.jl (3.7 KB)
mwe.c.jl (1.8 KB)