Skip to content

Windows: return the actual error when window creation fails - #377

Open
cjbhaines wants to merge 1 commit into
RustAudio:masterfrom
cjbhaines:fix/windows-create-error
Open

cjbhaines wants to merge 1 commit into
RustAudio:masterfrom
cjbhaines:fix/windows-create-error

Conversation

@cjbhaines

Copy link
Copy Markdown

I ran into this while testing a vizia change that makes a failed open_parented return an error instead of only logging it. On macOS the error that came back was the real reason, but on Windows it was some leftover GetLastError value: 0x80070002 once and 0 another time. baseview only sends the actual reason to the log, and only with the tracing feature on.

What happens: when WM_NCCREATE or WM_CREATE fails (say the GL context can't be created, or the window handler builder returns an error), handle_error_as_fatal logs the error and the window proc fails the message, so CreateWindowExW returns NULL. create_window then returns Error::from_thread(), but nothing in that path sets the thread's last error to the actual failure, so you get whatever happens to be there.

The fix: WindowData gets a slot for that error. handle_error_as_fatal still logs it, then puts it in the slot before dropping the window's reference to the data, and create_window returns it when CreateWindowExW gives back NULL. create_window holds its own Rc to the data, so it's still there to read. If the slot is empty, none of our steps failed, so CreateWindowExW failed on its own (a bad parent, say), and in that case GetLastError does mean something, so it still falls back to Error::from_thread().

Since create_window now returns a PlatformError instead of a windows_core::Error, the timer window code that also calls it (TimerList) returns PlatformResult as well, and create_timer drops an Ok(...?).

Tests: I added two Windows tests in src/platform/win/tests.rs. The first creates a window whose handler builder returns an error, and checks that the error coming out of Window::create has that same error as its source. It fails on master. The second creates a child window inside a parent that's already been destroyed, so CreateWindowExW fails on its own, and checks the error is still ERROR_INVALID_WINDOW_HANDLE from the fallback.

When WM_NCCREATE or WM_CREATE failed, the error was only logged, and
create_window returned Error::from_thread(), which is just whatever the
thread's last error happened to be. Keep the error in WindowData and
return it from create_window, falling back to GetLastError only when
CreateWindowExW failed on its own.

create_window now returns PlatformError, so TimerList follows.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant