Box from and to raw (SIGSEV - address not mapped to object)
⚓ Rust 📅 2025-11-16 👤 surdeus 👁️ 6Hello,
I'm using a function from C library in which I need to pass a callback function and some optional user data.
In my case I'm passing some user data. My rust user data is of type:
Arc<RwLock<SomeStruct>>
The code that sets the callback function looks like this:
let user_data = Box::new(self.data.clone());
unsafe { the_c_api_function(Self::my_callback_function, Box::into_raw(user_data));
In my callback I do:
let data = Box::from_raw(user_data)
let guard = callbacks.read().unwrap() // <-- Here it seems to crash (that's what the debugger seems to be showing)
Here is the stack:
- core::sync::atomic::atomic_load atomic.rs:3950
- [Inlined] core::sync::atomic::AtomicU32::load atomic.rs:2879
- std::sys::sync::rwlock::futex::RwLock::read futex.rs:95
- std::sync::poison::rwlock::RwLock::read rwlock.rs:394
- my_module
Thank you very much in advance for any help and time
1 post - 1 participant
🏷️ Rust_feed