Regardless whether the Handler::cb field is initialized as None or Some("extern "C" fn() -> i32 object") the following method code enters invariably into the Some(f) alternative:
pub fn handle_idling(&mut self) -> i32 {
let mut o: Option i32> = unsafe { (*self).cb };
let err = match o {
Some(f) => {
println!("We're trying the callback ...");
unsafe { f() };
}
None => {
println!("Skipping callback!");
0
}
};
err
}
In both cases (i.e. using and not using the callback function) the execution segfaults... Help much appreciated!
When you're asking for help, you need to be thorough and accurate. Your example code does not compile, even if I fill in the missing parts. Beyond that, there are numerous suspicious problems (such as the entirely needless unsafe blocks, and "extern "C" fn() -> i32 object" not meaning anything in context), which makes me wonder if this is even what the code looks like.
Here is a complete example that works perfectly. If there's a problem, it's somewhere else.
Look for any places you have unsafe. In particular, any place you construct or transport those values using unsafe or in code that Rust doesn't see. What you're doing should be safe, which suggests you're doing something inadvisable.
let mut o: Option i32> = unsafe { (*self.dh).cb };
which caused the problem, ...for reasons still unknown to me since I have an toy example (copied from the Internet) that works. Anyway, changing Handler into