rksm / hot-lib-reloader-rs

Reload Rust code without app restarts. For faster feedback cycles.
MIT License
597 stars 19 forks source link

`Egui`, `Windows`: hot-egui example crashes on reload if updated to eframe version > 0.19.0 #31

Open gabrieldechichi opened 1 year ago

gabrieldechichi commented 1 year ago

The hot-egui example crashes on reload, if eframe is updated to version any version above 0.19.0 (0.20.0, 0.21.0 or 0.22.0), with the following error:

Running `target\debug\hot-egui.exe`
error: process didn't exit successfully: `target\debug\hot-egui.exe` (exit code: 0xc000041d)

Machine specs:

OS: Windows 11 Home (10.0.22621)
CPU: AMD Ryzen 7 5800H
GPU: Geforce RTX 3600

Repro steps:

This is most likely due to a change on eframe, but I'm unsure how to debug this. Any suggestions?

branpk commented 1 year ago

Fwiw - I get a crash as described on Windows when using egui, but with egui-winit + egui-wgpu instead of eframe.

The solution is to make sure that egui isn't updating or rendering when the dll is reloaded. Here's how I did it in my case:

let mutex: Arc<Mutex<()>> = Arc::new(Mutex::new(()));

// once at start:
let observer = hot_reload::subscribe();
std::thread::spawn(move || loop {
    let block_reload = observer.wait_for_about_to_reload();
    let lock = mutex.lock().unwrap();
    drop(block_reload);
    observer.wait_for_reload();
});

// before rendering/updating the egui state:
let lock = mutex.lock().unwrap();
...

I think it'd be a little trickier if you don't own the main loop, but it should still be doable.

Edit: simplified and made the code more robust