tcod-rs icon indicating copy to clipboard operation
tcod-rs copied to clipboard

Panic when using check_for_event

Open Soupstraw opened this issue 4 years ago • 11 comments

I tried to use the check_for_event function, but got this error:

32bits font... checking for alpha layer... not present
key color : 0 0 0
thread 'main' panicked at 'attempted to leave type `bindings::ffi::TCOD_key_t` uninitialized, which is invalid', /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/mem/mod.rs:659:9
stack backtrace:
   0: rust_begin_unwind
             at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/std/src/panicking.rs:483:5
   1: core::panicking::panic_fmt
             at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/panicking.rs:85:14
   2: core::panicking::panic
             at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/panicking.rs:50:5
   3: core::mem::uninitialized
             at /rustc/ffa2e7ae8fbf9badc035740db949b9dae271c29f/library/core/src/mem/mod.rs:659:9
   4: tcod::input::check_for_event
             at [..]/.cargo/registry/src/github.com-1ecc6299db9ec823/tcod-0.15.0/src/input.rs:193:53
   5: rustrogue::handle_keys
             at ./src/main.rs:70:21
   6: rustrogue::main
             at ./src/main.rs:48:20
   7: core::ops::function::FnOnce::call_once
             at [..]/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

I am using the Rust nightly toolchain, if that's somehow related.

Soupstraw avatar Nov 07 '20 21:11 Soupstraw

This seems to be specific to the nightly toolchain, works fine with stable.

Soupstraw avatar Nov 07 '20 21:11 Soupstraw

I am getting the same error using the stable toolchain (stable-x86_64-pc-windows-msvc (default)). rustc 1.48.0 (7eac88abb 2020-11-16)

grkuntzmd avatar Dec 05 '20 16:12 grkuntzmd

Sorry for the delay in replying! I've been able to reproduce this on Linux with Rust rustc 1.48.0 (7eac88abb 2020-11-16) as well:

$ cargo run --example samples
thread 'main' panicked at 'attempted to leave type `bindings::ffi::TCOD_key_t` uninitialized, which is invalid', /rustc/7eac88abb2e57e752f3302f02be5f3ce3d7adfb4/library/core/src/mem/mod.rs:658:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

There's also this warning during compilation:

warning: use of deprecated function `std::mem::uninitialized`: use `mem::MaybeUninit` instead
   --> src/input.rs:193:53
    |
193 |     let mut c_key_state: ffi::TCOD_key_t = unsafe { mem::uninitialized() };

Switching all uses of unitialized!() will hopefully fix this. MaybeUninit. Unfortunately, I don't have the time/energy to maintain this project anymore. I've just opened a readme update that makes that explicit: #314.

If you open a fix, I'll merge it, but it may be better to try one of the newer and more powerful libraries instead:

  • https://github.com/thebracket/bracket-lib
  • https://github.com/jice-nospam/doryen-rs

tomassedovic avatar Dec 06 '20 22:12 tomassedovic

I've raised #315 if anyone's available to merge.

This replaces the deprecated calls with MaybeUninit. Tests all still pass and the examples now all seem to run properly.

ojhp avatar Jan 11 '21 18:01 ojhp

While looking at this I noticed there's a couple of other bits that are currently giving deprecated warnings that I'd guess will break in later versions of rust:

  • Inclusive ranges using ... instead of ..=
  • Something I don't understand in the lazy_static! macro that seems to be fixed by updating the dependency

If wanted I can push fixes for those too.

ojhp avatar Jan 11 '21 18:01 ojhp

Hi, switching to mem::MaybeUninit doesn't seem to do much, i get the same error on both stable and nightly toolchain

thread 'main' panicked at 'attempted to leave type `tcod_sys::TCOD_key_t` uninitialized, which is invalid', /home/rob/.cargo/registry/src/github.com-1ecc6299db9ec823/tcod-0.15.0/src/input.rs:193:53

Line 193:

let mut c_key_state: mem::MaybeUninit<ffi::TCOD_key_t> = mem::MaybeUninit::uninit();

Why not just using an Option<ffi::TCOD_key_t>?

rhighs avatar Nov 26 '21 15:11 rhighs

I'm hitting this by following the tutorial about Rogue Like.

https://tomassedovic.github.io/roguelike-tutorial/part-7-gui.html

(I'm putting this in case someone go through the tutorial and reach the same issue)

hfiguiere avatar Jan 30 '22 06:01 hfiguiere

For anyone that would like to finish the tutorial as exercise, switching to master branch in cargo worked for me (at least, for this specific issue).

[dependencies]
#tcod = "0.15"
tcod = { git= "https://github.com/tomassedovic/tcod-rs.git"}

giulio-Joshi avatar Feb 05 '22 10:02 giulio-Joshi

Switching to master seems to solve the error, but for some odd reason, there is an occasional input stick. It's like something isn't registering a key release...

Opayne93 avatar Mar 08 '22 04:03 Opayne93

Switching to master seems to solve the error, but for some odd reason, there is an occasional input stick. It's like something isn't registering a key release...

Getting the same error

Just-a-Unity-Dev avatar Sep 19 '22 08:09 Just-a-Unity-Dev

As @giulio-Joshi said above, I fixed this by changing my Cargo.toml to:

[dependencies]
tcod = { git = "https://github.com/tomassedovic/tcod-rs.git", rev = "d4ad074"}

john-science avatar Nov 11 '22 15:11 john-science