I have a C struct, wrapped in Rust, that takes a user-defined release function. However, I am getting a segfault because the function pointer is moving. I'm not sure what's going on here, and I'm new to unsafe rust and FFI's, any help would be greatly appreciated.
Here's my code so far:
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct Foo {
pub release_func: Option<unsafe extern "C" fn(data: gpointer, user_data: gpointer)>,
}
extern "C" fn user_meta_release_func(_data: gpointer, _user_data: gpointer) {
println!("release func");
}
// somewhere later:
let foo = Foo { release_func: Some(user_meta_release_func)};
When I first set release_func
, I see its value is Some(0x0000005555570780) in the debugger. But, when foo's drop trait is called, I then see that the value of release_func
is Some(0x3ff0000000000000).
The resulting error is:
Caused by:
process didn't exit successfully: `/ephemeral_data/monorail/target/debug/deps/deepstream-2ead9f4b11eca70c 'meta::tests::test_adding_user_meta_to_frame' --exact --nocapture` (signal: 11, SIGSEGV: invalid memory reference)
The terminal process "cargo 'test', '--package', 'deepstream', '--lib', '--', 'meta::tests::test_adding_user_meta_to_frame', '--exact', '--nocapture'" terminated with exit code: 101.
I'm not sure what's happening here, and might be attaching the callback incorrectly. I followed FFI - The Rustonomicon as best I could, but didn't see my exact use case as an example.