The following is a rough outline of my code on the rust side. These two functions are the only two that contain any references to CTX
#[repr(transparent)]
#[derive(Clone, Copy)]
pub struct CtxPtr(NonNull<OxideGLContext>>);
//Thread local
thread_local! {
pub static CTX: Cell<Option<CtxPtr>> = const {Cell::new(None)};
}
// Called from C
#[no_mangle]
extern "C" fn oxidegl_set_current_context(ctx: Option<CtxPtr>) {
println!(
"stace trace in set_context: {}",
std::backtrace::Backtrace::force_capture().to_string()
);
CTX.set(ctx);
// "set context Some(0xblahblah)"
println!("set context {:?}", ctx);
}
// Called from Rust called by C
fn get_state() -> CtxPtr {
println!(
"\n\nstack trace from get_state: {}",
std::backtrace::Backtrace::force_capture().to_string()
);
let c = CTX.get();
println!("got context {:?}", &c);
//panic: tried to unwrap None value
c.unwrap()
}
#[no_mangle]
extern "C" fn thing_that_uses_state() {
do_something(get_state());
}
This library is linked to a C binary that calls the functions in this pattern. I've confirmed that all of this is happening on the same thread
// create_context returns valid CtxPtr
void* ptr = oxidegl_create_context([irrelevant args]);
oxidegl_set_current_context(ptr);
// Causes panic in get_state, CTX is None????
thing_that_uses_state();
Any insight into this would be appreciated, I'm completely stumped
