I am teaching myself about the Rust FFI and have created a small example library to better understand how to allocate and free memory associated with trait objects. The allocation and freeing of the memory occurs through a C-API. The problem is that running the test program that uses this library results in a segmentation fault.

I can isolate the problem to three lines of code, but I do not know why they result in a segmentation fault. I have also found that the problematic lines of code actually work when they are part of the binary program’s code and not part of the library. Could anyone please explain these observations to me?

Thanks!
-kmd

Steps to reproduce

Create a new library crate with the command cargo new --lib ffi-test

Change into the ffi-test directory and replace the contents of the file src/lib.rs with the following: