17cupsofcoffee / tetra

🎮 A simple 2D game framework written in Rust
MIT License
909 stars 63 forks source link

crash with "Segmentation fault: 11" message when multiple gamepads connected. #281

Closed Im-Oab closed 3 years ago

Im-Oab commented 3 years ago

Summary

My game randomly crashes at the start of the runtime with the error message Segmentation fault: 11 and this error appear when 2 gamepads connected with the computer before running the game using cargo run

From the printing message. I assume that the game crash before enteringGameState::new()

Steps to Reproduce

Additional Info

OSX Catalina 10.15.7 tetra = "0.6.2" gamepads:

Im-Oab commented 3 years ago

This issue also appears in the tetra example/gamepad.

17cupsofcoffee commented 3 years ago

😬 noooooo

I feel like this is likely to be an issue in sdl2-rs or SDL2 itself - the only way you can get segmentation faults in Rust is via unsafe code, and I don't have any unsafe code involving gamepads as far as I can remember. Do you happen to know which version of SDL your game is using? If it's an old-ish one, I'd be interested to know if upgrading fixes this.

If that doesn't help, I will try to do some investigation over the weekend.

17cupsofcoffee commented 3 years ago

Just confirmed that there's only three places with unsafe in Tetra:

There is no unsafety anywhere relating to gamepads, so unless I've triggered some weird ripple-effect UB with one of the above bits of code, this has to be unsoundness somewhere in sdl2-rs. I will see if I can narrow it down further, and if I can find the cause I will file an upstream bug report.

17cupsofcoffee commented 3 years ago

I haven't been able to replicate this yet as my only other controllers are joysticks/steering wheels, and SDL does not register them as gamepads.

I have pushed a branch with some debug statements added, to try to see where during startup the segfault is happening - could you try with this dependency and let me know what it prints @Im-Oab?

tetra = { git = "https://github.com/17cupsofcoffee/tetra", branch = "gamepad-debug" }
Im-Oab commented 3 years ago

This is my SDL2 version. Screen Shot 2021-09-11 at 3 47 20 am

and I set up a new project, copy the examples folder from gamepad-debug branch. I got this message after call cargo run --example gamepad Screen Shot 2021-09-11 at 3 45 05 am

17cupsofcoffee commented 3 years ago

Ah, that's an unrelated issue - tetra 0.6.5 added some code that uses const generics, which were added to stable Rust in March. Running rustup update should make that error go away, but in case you need to stay on an old version for whatever reason, I've pushed a commit to the branch to comment that code out.

The SDL version looks fine to me - 2.0.14 is only one release behind the latest (and I haven't updated to the latest yet either).

Im-Oab commented 3 years ago

After I did rustup update. the error gone.

Screen Shot 2021-09-11 at 4 00 20 am

Im-Oab commented 3 years ago

The segmentation fault error change after I called cargo run --example gamepad again and again.

Screen Shot 2021-09-11 at 4 14 16 am

17cupsofcoffee commented 3 years ago

That's extremely strange, it's crashing right at startup by the looks of it - which is before we actually do anything with the controllers themselves... I have a feeling this might be an SDL2 bug.

Just out of interest, are you always plugging the gamepads in the same order? I wonder if it's something to do with SDL not playing nice with one of those controllers in particular.

I will try to see if I can get my steering wheel controller to be picked up by SDL, just remembered it has a PS4 mode that might make it register as a gamepad...

Im-Oab commented 3 years ago

Yes. It is the same order when i try to reproduce the bug.

  1. USB gamepad always plug in.
  2. Turn on bluetooth gamepad to make error happen.
17cupsofcoffee commented 3 years ago

I just found this patch on the SDL2 issue tracker that looks like it could be relevant, given that it mentions Macs, multiple gamepads and an 8BitDo controller: https://github.com/libsdl-org/SDL/commit/bdc6e4ffc5db34940b11bb63e2379bcf194ed990

That bug was fixed in 2.0.16, which is one release newer than the one you've got now. Could you try updating your SDL version via Homebrew and doing a clean build? Hopefully that'll fix your issue 🤞

Even if it doesn't, I'm fairly convinced this error must be coming from SDL2 rather than the Rust bindings or Tetra, as it's happening so early in initialization.

Im-Oab commented 3 years ago

Thanks you. The error is gone after I updated it to 2.0.16.

17cupsofcoffee commented 3 years ago

No problem, thanks for the help debugging this :)