Skip to content

Commit d6c1b36

Browse files
fix issues with imgui backend/renderer cleanup (scp-fs2open#7278)
If the graphics system fails to fully init for some reason then it's possible for the imgui backend and/or renderer to not be initialized. Those subsystems *must* be initialized before being shut down so we need to check for that to avoid hitting assertions inside of imgui. Also add a safety check for making sure that an imgui context exists before attempting to initialize the backend/renderer. A simple assertion should work for this as it would take a coder messing something up in order to trip it.
1 parent 3c3a385 commit d6c1b36

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

freespace2/SDLGraphicsOperations.cpp

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,18 @@ SDLGraphicsOperations::SDLGraphicsOperations() {
154154
}
155155
}
156156
SDLGraphicsOperations::~SDLGraphicsOperations() {
157-
SDL_QuitSubSystem(SDL_INIT_VIDEO);
158-
159-
ImGui_ImplSDL2_Shutdown();
157+
// make sure imgui stuff is initialized before trying to shut it down
158+
if (ImGui::GetCurrentContext()) {
159+
if (ImGui::GetIO().BackendPlatformUserData) {
160+
ImGui_ImplSDL2_Shutdown();
161+
}
160162

161-
if (!Cmdline_vulkan) {
162-
ImGui_ImplOpenGL3_Shutdown();
163+
if ( !Cmdline_vulkan && ImGui::GetIO().BackendRendererUserData ) {
164+
ImGui_ImplOpenGL3_Shutdown();
165+
}
163166
}
167+
168+
SDL_QuitSubSystem(SDL_INIT_VIDEO);
164169
}
165170
std::unique_ptr<os::Viewport> SDLGraphicsOperations::createViewport(const os::ViewPortProperties& props)
166171
{
@@ -267,7 +272,8 @@ std::unique_ptr<os::OpenGLContext> SDLGraphicsOperations::createOpenGLContext(os
267272
mprintf((" Actual SDL Video values = R: %d, G: %d, B: %d, depth: %d, stencil: %d, double-buffer: %d, FSAA: %d\n",
268273
r, g, b, depth, stencil, db, fsaa_samples));
269274

270-
275+
Assertion(ImGui::GetCurrentContext() != nullptr, "Can't use ImGui without a valid context!");
276+
271277
ImGui_ImplSDL2_InitForOpenGL(viewport->toSDLWindow(), ctx);
272278
ImGui_ImplOpenGL3_Init();
273279

0 commit comments

Comments
 (0)