Docking+Viewports: NULL Viewport crash in UpdateTryMergeWindowIntoHostViewport() when re-opening a window docked in a hidden floating node #9631

Closed
opened 2026-11-18 13:55:49 +00:00 by fabrisdnl · 0 comments
fabrisdnl commented 2026-11-18 13:55:49 +00:00 (Migrated from github.com)

Version/Branch of Dear ImGui:

1.92.4, docking

Back-ends:

imgui_impl_win32.cpp + imgui_impl_opengl3.cpp (multi-viewport renderer hooks as in example_win32_opengl3)

Compiler, OS:

Windows 11 24H2 (build 26100) + MSVC 2022 17.14 (19.44.35219)

Full config/build information:

Dear ImGui 1.92.4 (19240)
--------------------------------

sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20
define: __cplusplus=199711
define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS
define: _WIN32
define: _WIN64
define: _MSC_VER=1944
define: _MSVC_LANG=201402
define: IMGUI_HAS_VIEWPORT
define: IMGUI_HAS_DOCK
--------------------------------

io.BackendPlatformName: imgui_impl_win32
io.BackendRendererName: imgui_impl_opengl3
io.ConfigFlags: 0x00000483
 NavEnableKeyboard
 NavEnableGamepad
 DockingEnable
 ViewportsEnable
io.ConfigDpiScaleFonts
io.ConfigDpiScaleViewports
io.ConfigViewportsNoDecoration
io.ConfigViewportsNoDefaultParent
io.ConfigNavCaptureKeyboard
io.ConfigInputTextCursorBlink
io.ConfigWindowsResizeFromEdges
io.ConfigWindowsMoveFromTitleBarOnly
io.ConfigMemoryCompactTimer = 60.0
io.BackendFlags: 0x00003C1E
 HasMouseCursors
 HasSetMousePos
 PlatformHasViewports
 HasMouseHoveredViewport
 HasParentViewport
 RendererHasVtxOffset
 RendererHasTextures
 RendererHasViewports
--------------------------------

io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128
io.Fonts->FontLoaderName: FreeType
io.DisplaySize: 2560.00,1528.00
io.DisplayFramebufferScale: 1.00,1.00
--------------------------------

style.WindowPadding: 15.00,15.00
style.WindowBorderSize: 1.00
style.FramePadding: 5.00,5.00
style.FrameRounding: 4.00
style.FrameBorderSize: 0.00
style.ItemSpacing: 12.00,8.00
style.ItemInnerSpacing: 8.00,6.00

Details:

My Issue/Question:

Access violation reading 0x70 (ImGuiViewportP::Window) in SetWindowViewport() with window->Viewport == NULL. Release build, IM_ASSERT disabled.
Config: DockingEnable | ViewportsEnable | NavEnableKeyboard | NavEnableGamepad, ConfigDpiScaleFonts = ConfigDpiScaleViewports = true.

Callstack (4 crash dumps, all identical):

SetWindowViewport()                      (inlined) window->Viewport == NULL, ViewportOwned == true
UpdateTryMergeWindowIntoHostViewport()   loop over g.Windows with old_viewport == NULL
WindowSelectViewport()                   "Fallback: merge in default viewport"
ImGui::Begin()

Repro steps

  1. Dock two windows A and B side by side (split, not tabs) in a floating dock node.
  2. Move the node so that it partially leaves the main viewport (it gets its own platform window), while A's half is still inside the main viewport rect.
  3. Close A (its half becomes an empty dock node), then close B. The host window is hidden, but the node keeps HostWindow pointing to it. Two frames later the host viewport is destroyed, so HostWindow->Viewport == NULL.
  4. Re-open A -> crash.

If A's rect is not contained in the main viewport, the merge returns early and a new viewport is created: no crash.

static bool show_a = true, show_b = true;
ImGui::Begin("Control");
ImGui::Checkbox("A", &show_a);
ImGui::Checkbox("B", &show_b);
ImGui::End();
if (show_a) { ImGui::Begin("A", &show_a); ImGui::Text("A"); ImGui::End(); }
if (show_b) { ImGui::Begin("B", &show_b); ImGui::Text("B"); ImGui::End(); }

What happens in WindowSelectViewport() for A

  • ViewportOwned = false, Viewport == NULL, ViewportId still refers to the destroyed viewport, so the restore block calls AddUpdateViewport(), which sets ViewportOwned = true.
  • A is docked (ImGuiWindowFlags_ChildWindow): window->Viewport = window->ParentWindow->Viewport -> NULL, ViewportOwned stays true.
  • Fallback UpdateTryMergeWindowIntoHostViewport(window, main_viewport): with ViewportOwned and old_viewport == NULL, the loop calls SetWindowViewport() on every window with a NULL viewport, A included -> window->Viewport->Window.

Local workaround

We are not sure this is the right place to fix it. The stale HostWindow of the empty node may be the actual root cause.

@@ void ImGui::SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport)
-    if (window->ViewportOwned && window->Viewport->Window == window)
+    if (window->ViewportOwned && window->Viewport != NULL && window->Viewport->Window == window)
@@ static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport)
-    if (window->ViewportOwned)
+    if (window->ViewportOwned && old_viewport != NULL)
@@ static void ImGui::WindowSelectViewport(ImGuiWindow* window)
         window->Viewport = window->ParentWindow->Viewport;
+        window->ViewportOwned = false;

Screenshots/Video:

No response

Minimal, Complete and Verifiable Example code:

// Here's some code anyone can copy and paste to reproduce your issue
ImGui::Begin("Example Bug");
MoreCodeToExplainMyIssue();
ImGui::End();
### Version/Branch of Dear ImGui: 1.92.4, docking ### Back-ends: imgui_impl_win32.cpp + imgui_impl_opengl3.cpp (multi-viewport renderer hooks as in example_win32_opengl3) ### Compiler, OS: Windows 11 24H2 (build 26100) + MSVC 2022 17.14 (19.44.35219) ### Full config/build information: ```cpp Dear ImGui 1.92.4 (19240) -------------------------------- sizeof(size_t): 8, sizeof(ImDrawIdx): 2, sizeof(ImDrawVert): 20 define: __cplusplus=199711 define: IMGUI_DISABLE_OBSOLETE_FUNCTIONS define: _WIN32 define: _WIN64 define: _MSC_VER=1944 define: _MSVC_LANG=201402 define: IMGUI_HAS_VIEWPORT define: IMGUI_HAS_DOCK -------------------------------- io.BackendPlatformName: imgui_impl_win32 io.BackendRendererName: imgui_impl_opengl3 io.ConfigFlags: 0x00000483 NavEnableKeyboard NavEnableGamepad DockingEnable ViewportsEnable io.ConfigDpiScaleFonts io.ConfigDpiScaleViewports io.ConfigViewportsNoDecoration io.ConfigViewportsNoDefaultParent io.ConfigNavCaptureKeyboard io.ConfigInputTextCursorBlink io.ConfigWindowsResizeFromEdges io.ConfigWindowsMoveFromTitleBarOnly io.ConfigMemoryCompactTimer = 60.0 io.BackendFlags: 0x00003C1E HasMouseCursors HasSetMousePos PlatformHasViewports HasMouseHoveredViewport HasParentViewport RendererHasVtxOffset RendererHasTextures RendererHasViewports -------------------------------- io.Fonts: 1 fonts, Flags: 0x00000000, TexSize: 512,128 io.Fonts->FontLoaderName: FreeType io.DisplaySize: 2560.00,1528.00 io.DisplayFramebufferScale: 1.00,1.00 -------------------------------- style.WindowPadding: 15.00,15.00 style.WindowBorderSize: 1.00 style.FramePadding: 5.00,5.00 style.FrameRounding: 4.00 style.FrameBorderSize: 0.00 style.ItemSpacing: 12.00,8.00 style.ItemInnerSpacing: 8.00,6.00 ``` ### Details: **My Issue/Question:** Access violation reading 0x70 (`ImGuiViewportP::Window`) in `SetWindowViewport()` with `window->Viewport == NULL`. Release build, IM_ASSERT disabled. Config: `DockingEnable | ViewportsEnable | NavEnableKeyboard | NavEnableGamepad`, `ConfigDpiScaleFonts = ConfigDpiScaleViewports = true`. Callstack (4 crash dumps, all identical): ``` SetWindowViewport() (inlined) window->Viewport == NULL, ViewportOwned == true UpdateTryMergeWindowIntoHostViewport() loop over g.Windows with old_viewport == NULL WindowSelectViewport() "Fallback: merge in default viewport" ImGui::Begin() ``` **Repro steps** 1. Dock two windows A and B side by side (split, not tabs) in a floating dock node. 2. Move the node so that it partially leaves the main viewport (it gets its own platform window), while A's half is still inside the main viewport rect. 3. Close A (its half becomes an empty dock node), then close B. The host window is hidden, but the node keeps `HostWindow` pointing to it. Two frames later the host viewport is destroyed, so `HostWindow->Viewport == NULL`. 4. Re-open A -> crash. If A's rect is not contained in the main viewport, the merge returns early and a new viewport is created: no crash. ```cpp static bool show_a = true, show_b = true; ImGui::Begin("Control"); ImGui::Checkbox("A", &show_a); ImGui::Checkbox("B", &show_b); ImGui::End(); if (show_a) { ImGui::Begin("A", &show_a); ImGui::Text("A"); ImGui::End(); } if (show_b) { ImGui::Begin("B", &show_b); ImGui::Text("B"); ImGui::End(); } ``` **What happens in WindowSelectViewport() for A** - `ViewportOwned = false`, `Viewport == NULL`, `ViewportId` still refers to the destroyed viewport, so the restore block calls `AddUpdateViewport()`, which sets `ViewportOwned = true`. - A is docked (`ImGuiWindowFlags_ChildWindow`): `window->Viewport = window->ParentWindow->Viewport` -> NULL, `ViewportOwned` stays true. - Fallback `UpdateTryMergeWindowIntoHostViewport(window, main_viewport)`: with `ViewportOwned` and `old_viewport == NULL`, the loop calls `SetWindowViewport()` on every window with a NULL viewport, A included -> `window->Viewport->Window`. **Local workaround** We are not sure this is the right place to fix it. The stale `HostWindow` of the empty node may be the actual root cause. ```diff @@ void ImGui::SetWindowViewport(ImGuiWindow* window, ImGuiViewportP* viewport) - if (window->ViewportOwned && window->Viewport->Window == window) + if (window->ViewportOwned && window->Viewport != NULL && window->Viewport->Window == window) @@ static bool ImGui::UpdateTryMergeWindowIntoHostViewport(ImGuiWindow* window, ImGuiViewportP* viewport) - if (window->ViewportOwned) + if (window->ViewportOwned && old_viewport != NULL) @@ static void ImGui::WindowSelectViewport(ImGuiWindow* window) window->Viewport = window->ParentWindow->Viewport; + window->ViewportOwned = false; ``` ### Screenshots/Video: _No response_ ### Minimal, Complete and Verifiable Example code: ```cpp // Here's some code anyone can copy and paste to reproduce your issue ImGui::Begin("Example Bug"); MoreCodeToExplainMyIssue(); ImGui::End(); ```
Sign in to join this conversation.
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ocornut/imgui#9631
No description provided.