The following snippet, when added to a windows-rs program, causes a STATUS_ACCESS_VIOLATION on exit. If I negate the conditional check, no error occurs. I can only assume that there is some kind of memory corruption occurring as a result of a controller actually being connected, but I'm at a loss as to why or how to fix it.

for i in 0..XUSER_MAX_COUNT {
    let mut controller_state = unsafe { std::mem::zeroed::<XINPUT_STATE>() };
    if unsafe { XInputGetState(i, &mut controller_state) == ERROR_SUCCESS.0 } {
        println!("{} {:?}", i, controller_state);
    } else {
        // Controller is not connected.


0 XINPUT_STATE { dwPacketNumber: 11, Gamepad: XINPUT_GAMEPAD { wButtons: 0, bLeftTrigger: 0, bRightTrigger: 0, sThumbLX: -32768, sThumbLY: 0, sThumbRX: 0, sThumbRY: 0 } }
error: process didn't exit successfully: `target\debug\horizon.exe` (exit code: 0xc0000005, STATUS_ACCESS_VIOLATION)

A full example is available. Is anyone able to help explain the cause and suggest a fix?