Browse Source

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)

omar 7 years ago
parent
commit
e026c8d3b7

+ 1 - 0
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));
 }

+ 1 - 0
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));
 }

+ 1 - 0
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));
 }

+ 1 - 0
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));
 }

+ 16 - 7
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);
     }

+ 2 - 0
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);