From e026c8d3b7bce7abfccbd1014110a37d80ddfc35 Mon Sep 17 00:00:00 2001 From: omar Date: Sat, 3 Mar 2018 22:53:31 +0100 Subject: [PATCH] Viewport: Give a chance to platform and renderer to actually destroy their data. Otherwise the regular ImplXXX_Shutdown + following by DestroyContext() order fails to fullifl this. (#1542) --- examples/imgui_impl_dx10.cpp | 1 + examples/imgui_impl_dx11.cpp | 1 + examples/imgui_impl_dx12.cpp | 1 + examples/imgui_impl_opengl3.cpp | 1 + imgui.cpp | 23 ++++++++++++++++------- imgui_internal.h | 2 ++ 6 files changed, 22 insertions(+), 7 deletions(-) diff --git a/examples/imgui_impl_dx10.cpp b/examples/imgui_impl_dx10.cpp index d88e2cc8..014c4c37 100644 --- a/examples/imgui_impl_dx10.cpp +++ b/examples/imgui_impl_dx10.cpp @@ -611,6 +611,7 @@ void ImGui_ImplDX10_InitPlatformInterface() void ImGui_ImplDX10_ShutdownPlatformInterface() { + ImGui::DestroyViewportsRendererData(ImGui::GetCurrentContext()); ImGuiIO& io = ImGui::GetIO(); memset(&io.RendererInterface, 0, sizeof(io.RendererInterface)); } diff --git a/examples/imgui_impl_dx11.cpp b/examples/imgui_impl_dx11.cpp index b7c26cd3..56fc7f6a 100644 --- a/examples/imgui_impl_dx11.cpp +++ b/examples/imgui_impl_dx11.cpp @@ -619,6 +619,7 @@ void ImGui_ImplDX11_InitPlatformInterface() void ImGui_ImplDX11_ShutdownPlatformInterface() { + ImGui::DestroyViewportsRendererData(ImGui::GetCurrentContext()); ImGuiIO& io = ImGui::GetIO(); memset(&io.RendererInterface, 0, sizeof(io.RendererInterface)); } diff --git a/examples/imgui_impl_dx12.cpp b/examples/imgui_impl_dx12.cpp index 7ed9a394..6de48c62 100644 --- a/examples/imgui_impl_dx12.cpp +++ b/examples/imgui_impl_dx12.cpp @@ -763,6 +763,7 @@ void ImGui_ImplDX12_InitPlatformInterface() void ImGui_ImplDX12_ShutdownPlatformInterface() { + ImGui::DestroyViewportsRendererData(ImGui::GetCurrentContext()); ImGuiIO& io = ImGui::GetIO(); memset(&io.RendererInterface, 0, sizeof(io.RendererInterface)); } diff --git a/examples/imgui_impl_opengl3.cpp b/examples/imgui_impl_opengl3.cpp index d2a459dd..91d363f2 100644 --- a/examples/imgui_impl_opengl3.cpp +++ b/examples/imgui_impl_opengl3.cpp @@ -327,6 +327,7 @@ void ImGui_ImplOpenGL3_InitPlatformInterface() void ImGui_ImplOpenGL3_ShutdownPlatformInterface() { + ImGui::DestroyViewportsRendererData(ImGui::GetCurrentContext()); ImGuiIO& io = ImGui::GetIO(); memset(&io.RendererInterface, 0, sizeof(io.RendererInterface)); } diff --git a/imgui.cpp b/imgui.cpp index 7ee87a36..f6e099c9 100644 --- a/imgui.cpp +++ b/imgui.cpp @@ -3909,6 +3909,20 @@ void ImGui::Initialize(ImGuiContext* context) g.Initialized = true; } +void ImGui::DestroyViewportsPlaformData(ImGuiContext* context) +{ + if (context->IO.PlatformInterface.DestroyViewport) + for (int i = 0; i < context->Viewports.Size; i++) + context->IO.PlatformInterface.DestroyViewport(context->Viewports[i]); +} + +void ImGui::DestroyViewportsRendererData(ImGuiContext* context) +{ + if (context->IO.RendererInterface.DestroyViewport) + for (int i = 0; i < context->Viewports.Size; i++) + context->IO.RendererInterface.DestroyViewport(context->Viewports[i]); +} + // This function is merely here to free heap allocations. void ImGui::Shutdown(ImGuiContext* context) { @@ -3945,16 +3959,11 @@ void ImGui::Shutdown(ImGuiContext* context) g.OpenPopupStack.clear(); g.CurrentPopupStack.clear(); g.MouseViewport = g.MouseLastHoveredViewport = NULL; + DestroyViewportsPlaformData(context); + DestroyViewportsRendererData(context); for (int i = 0; i < g.Viewports.Size; i++) { ImGuiViewport* viewport = g.Viewports[i]; - if (!(viewport->Flags & ImGuiViewportFlags_MainViewport)) // FIXME-VIEWPORT - { - if (g.IO.RendererInterface.DestroyViewport) - g.IO.RendererInterface.DestroyViewport(viewport); - if (g.IO.PlatformInterface.DestroyViewport) - g.IO.PlatformInterface.DestroyViewport(viewport); - } viewport->PlatformUserData = viewport->PlatformHandle = viewport->RendererUserData = NULL; IM_DELETE(viewport); } diff --git a/imgui_internal.h b/imgui_internal.h index 7c297f6a..cca7ff8f 100644 --- a/imgui_internal.h +++ b/imgui_internal.h @@ -1093,6 +1093,8 @@ namespace ImGui IMGUI_API ImGuiViewport* FindViewportByPlatformHandle(void* platform_handle); IMGUI_API void SetNextWindowViewport(ImGuiID id); IMGUI_API void ShowViewportThumbnails(); + IMGUI_API void DestroyViewportsPlaformData(ImGuiContext* context); + IMGUI_API void DestroyViewportsRendererData(ImGuiContext* context); IMGUI_API void MarkIniSettingsDirty(); IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name);