Skip to content

fix(windows): destroy window before quitting message loop - #590

Closed
Davit-Meliqyan wants to merge 2 commits into
leanflutter:mainfrom
Davit-Meliqyan:fix/windows-destroy-shutdown
Closed

Davit-Meliqyan wants to merge 2 commits into
leanflutter:mainfrom
Davit-Meliqyan:fix/windows-destroy-shutdown

Conversation

@Davit-Meliqyan

Copy link
Copy Markdown

Summary

Fixes a Windows shutdown delay when setPreventClose(true) is enabled.

Problem

windowManager.destroy() currently calls:

PostQuitMessage(0);

This exits the Win32 message loop before the native window is actually destroyed.

As a result, Flutter Engine shutdown happens outside the normal window lifecycle and the process may remain alive for around 17–18 seconds before exiting.

Solution

Replace:

PostQuitMessage(0);

with:

HWND hWnd = GetMainWindow();
is_prevent_close_ = false;
DestroyWindow(hWnd);

This restores the normal Win32 shutdown sequence:

WM_CLOSE
→ DestroyWindow
→ WM_DESTROY
→ FlutterWindow::OnDestroy()
→ Flutter Engine shutdown
→ PostQuitMessage

Verification

Tested on Windows 11.

Before:

  • Closing the application with setPreventClose(true) caused a ~17–18 second delay before process exit.

After:

  • Application exits immediately in both Debug and Release builds.

@lijy91

lijy91 commented Sep 21, 2026

Copy link
Copy Markdown
Member

Thank you for the PR, and sorry it sat here so long. window_manager 0.5.x is no longer maintained: from 0.6.0 window_manager is rebuilt on nativeapi (Flutter package: nativeapi), and the platform code in this repo's main branch goes away, so I'm closing PRs against it.

The same problem exists in nativeapi: Application::Quit on Windows also just calls PostQuitMessage (src/platform/windows/application_windows.cpp). A PR there that destroys the window before leaving the message loop would be very welcome.

@lijy91

lijy91 commented Sep 21, 2026

Copy link
Copy Markdown
Member

Follow-up: this is now tracked in libnativeapi/nativeapi#57. If you'd like to port the fix there, it's very welcome.

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.

2 participants