tomassedovic / tcod-rs

Rust bindings for libtcod 1.6.3 (the Doryen library/roguelike toolkit)
Do What The F*ck You Want To Public License
228 stars 45 forks source link

Panic when using check_for_event #313

Open Soupstraw opened 4 years ago

Soupstraw commented 4 years ago

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 commented 4 years ago

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

grkuntzmd commented 4 years ago

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)

tomassedovic commented 3 years ago

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:

ojhp commented 3 years ago

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 commented 3 years ago

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:

If wanted I can push fixes for those too.

rhighs commented 3 years ago

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>?

hfiguiere commented 2 years ago

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)

giulio-Joshi commented 2 years ago

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"}
Opayne93 commented 2 years ago

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...

Just-a-Unity-Dev commented 2 years ago

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

john-science commented 2 years ago

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"}