Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix crash when changing viewport settings #4862

Merged
merged 4 commits into from
Aug 26, 2024
Merged

Conversation

pm100
Copy link
Contributor

@pm100 pm100 commented Jul 22, 2024

There are two bugs racing each other here, which is why it sometimes crashes and sometimes the app just silently exists

Bug 1
When the window is recreated a Destroyed event arrives (due to the Drop of the old window). The code that receives this event does not look to see if its the main viewport or a secondary one and unconditionally closes the app. The code path for other platforms is slightly different and does check.

I have moved the code that handles the destroy to be in the same place and have the same behavior as the other platforms.

Bug 2

At recreate time the window and winit entries of the viewport are set to None (forcin g them to be recreated). But the surface is still bound to the old window, this causes the next context switch to fail. So I simply added a viewport.gl_surface = None too,

This is my first egui PR so I hope I have not broken anything. If nothing else I understand a little better how egui works.

@pm100
Copy link
Contributor Author

pm100 commented Jul 22, 2024

fix works on mac - it showed the same behavior as bug1, immediate exit when recreating secondary viewport. Did no exhibit bug 2

I cannot get egui to build on linux so cannot test

@emilk emilk changed the title Fix for #3959 Fix crash when changing viewport settings Jul 30, 2024
Comment on lines +832 to +843
winit::event::WindowEvent::Destroyed => {
log::debug!(
"Received WindowEvent::Destroyed for viewport {:?}",
viewport_id
);
if viewport_id == Some(ViewportId::ROOT) {
return EventResult::Exit;
} else {
return EventResult::Wait;
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably need to add the same code to wgpu_integration.rs.

We just merged the update to latest winit (#4849), so please merge that into this branch and then test this PR with:

  • cargo r -p test_viewports
  • cargo r -p test_viewports -F wgpu
  • cargo r -p serial_windows

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will do, note that I found this #4894

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emilk Am I reading this correctly. That clicking the close box on a child viewport does nothing unless I actually have code that checks for CloseRequested? And that you have to set the vp as not visible in order to destroy it . Thats not at all obvious. (Was trying to work out why the bug reporters app was not closing the child VP after I fixed the bugs I could find)

Copy link
Contributor Author

@pm100 pm100 Aug 3, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@emilk I added to wgpu the equivalent release of the drawing surface, in wgpu not releasing this resulted in the recreate of the vp leaving the original in place (rather than crashing as in glow).

Tested on windows and mac,

Note the wgpu has very strange defferred vp close behavior (also meaning that I did not need to add any close logic to wpgpu_integration). Because there is no close option only the option to no longer request its presence there is some very odd stuff happening. I worried that it was my bad but the current master shows the same thing. After CloseRequest is received you have to stop drawing secondary vp in update, BUT, this means you need another main window draw before the window disappears. I filed a bug on this #4894 . I tried to add the necessary code to the original reports code but could not - I thought it was simply a matter of doing request_repaint but I could not make that work. And the fact that the test_viewports app shows the same bug shows that its not simple.

I think it would be really ice to have an explicit vp.close call (even though that goes again the immediate model VPs are really not transient, the OS knows them (unlike stuff drawn on the gl surfaces). I will investigate and make a separate PR

Also I have not tested on linux becasue I cannot make it build on linux, it complains about a lot of pkg errors.

I will do a separate PR to add vp calls the need window recreation to test_viewports. Done #4907

In summary - I think this PR is complete, apart from not being tested on Linux.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

Comment on lines +832 to +843
winit::event::WindowEvent::Destroyed => {
log::debug!(
"Received WindowEvent::Destroyed for viewport {:?}",
viewport_id
);
if viewport_id == Some(ViewportId::ROOT) {
return EventResult::Exit;
} else {
return EventResult::Wait;
}
}

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@emilk emilk added bug Something is broken eframe Relates to epi and eframe viewports multiple viewports, viewports API labels Aug 26, 2024
emilk pushed a commit that referenced this pull request Aug 26, 2024
This make the test excercise the window recreation logic, that resulted
in several bugs - see #4862

Adds a check box that turns the close button on and off for child
windows
@emilk emilk merged commit 0c528fb into emilk:master Aug 26, 2024
22 checks passed
486c pushed a commit to 486c/egui that referenced this pull request Oct 9, 2024
This make the test excercise the window recreation logic, that resulted
in several bugs - see emilk#4862

Adds a check box that turns the close button on and off for child
windows
486c pushed a commit to 486c/egui that referenced this pull request Oct 9, 2024
* Fixes emilk#3959

There are two bugs racing each other here, which is why it sometimes
crashes and sometimes the app just silently exists

Bug 1
When the window is recreated a Destroyed event arrives (due to the Drop
of the old window). The code that receives this event does not look to
see if its the main viewport or a secondary one and unconditionally
closes the app. The code path for other platforms is slightly different
and does check.

I have moved the code that handles the destroy to be in the same place
and have the same behavior as the other platforms.

Bug 2

At recreate time the window and winit entries of the viewport are set to
None (forcin g them to be recreated). But the surface is still bound to
the old window, this causes the next context switch to fail. So I simply
added a viewport.gl_surface = None too,


This is my first egui PR so I hope I have not broken anything. If
nothing else I understand a little better how egui works.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is broken eframe Relates to epi and eframe viewports multiple viewports, viewports API
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Crash when changing a viewport aspect, that requires window recreation
2 participants