rust-qt / ritual

Use C++ libraries from Rust
Apache License 2.0
1.22k stars 49 forks source link

[Question] how to make it safe? #125

Open hasezoey opened 2 years ago

hasezoey commented 2 years ago

i ask this here, because github discussions seem to not be enabled for this project


i am still quite new to rust and have no experience in c/c++ (or other low-level / systems languages), and with this project i have to use a lot of unsafe and i also read Unsafety, but i could not understand how i could make it (more) safe, especially the following part got me thinking on how to do it properly:

Rust documentation for Qptr:

It's still possible to cause use-after-free by calling a method through QPtr. Even in a single threaded program, the accessed object can be deleted by a nested call while one of its methods is still running. In multithreaded context, the object can be deleted in another thread between the null check and the method call, also resulting in undefined behavior.

so my question is: how can i make it safe if it may not be safe after having already checked (like with is_null)? or how can i at least detect that it is wrong / malfunctioned and catch it to recover from it (like a Result in rust)?