this post was submitted on 10 Aug 2023
1 points (100.0% liked)

The Rust Programming Language

1 readers
0 users here now

A place for all things related to the Rust programming language—an open-source systems language that emphasizes performance, reliability, and...

founded 1 year ago
MODERATORS
 
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

no comments (yet)
sorted by: hot top controversial new old
there doesn't seem to be anything here