LeoAdamek / iracing.rs

Rust library to connect to iRacing telemetry
MIT License
20 stars 4 forks source link

Potential error in the close handle code. #14

Closed davide-scalzo closed 2 years ago

davide-scalzo commented 2 years ago

I'm new to Rust so feel free to disregard as an actual issue, but shouldn't CloseHandle be executed on mapping instead of view? https://github.com/LeoAdamek/iracing.rs/blob/9c10904f3f17adf53685e932f02fe731a240f60a/src/telemetry.rs#L751

I've taken your code as an inspiration to write my own ACC bindings, here is my code:

    pub fn close(&self) {
        match self.handle { // my naming for your mapping variable
            Some(v) => {
                let succ = unsafe { CloseHandle(v) };
                if succ != 0 {
                    println!("Handle closed succesfully");
                }  else {
                    let errno: i32 = unsafe { GetLastError() as i32 };
                    let message = std::io::Error::from_raw_os_error(errno);
                    println!("Failed to close handle: {}", message);
                }
            },
            None => {
                println!("No handle opened. Skipping.");
            }
        }
    }

So, I called my references handle in place of your mapping and view in place of your location. If I call CloseHandle on self.view (your location) is errors with Failed to close handle: The handle is invalid. (os error 6) but if I call it on self.handle (your mapping) exits successfully.

davide-scalzo commented 2 years ago

Oh I just realised this was covered in an earlier and more comprehensive issue.... disregard :D