This is an automated archive made by the Lemmit Bot.
The original was posted on /r/rust by /u/scorpionCPP on 2023-08-10 20:05:43.
Hi, I am working with C library bindings and needed to make some workaround to share data
pointer between threads in C way. I find out that Box
needs to be feeded again with pointer or copy to still be valid. That’s ok, but why after giving ownership to the raw pointer, Box
is printing “old” data insead of simply panic. It seems would be hard to investigate on production.
Code:
let data: Arc>>> = Arc::new(Mutex::new(Some(Box::new(42))));
{ let lock = data.lock().unwrap();
let data_ptr: *mut u8 = lock.clone()
.map(|boxed_u8| Box::into_raw(boxed_u8))
.unwrap_or(std::ptr::null_mut());
unsafe{
let ptr_copy: *mut u8 = data_ptr;
*ptr_copy = 54;
println!("{}", *ptr_copy);
}
}
let lock = data.lock().unwrap(); println!(“{}”, *lock.as_ref().unwrap());
Output:
54 42
You must log in or register to comment.