Browse Source

Add defensive context check to PersistentRingBuffer and simplify shutdown

Co-authored-by: djeada <[email protected]>
copilot-swe-agent[bot] 1 month ago
parent
commit
437c02a987
2 changed files with 13 additions and 8 deletions
  1. 3 8
      main.cpp
  2. 10 0
      render/gl/persistent_buffer.h

+ 3 - 8
main.cpp

@@ -369,19 +369,14 @@ auto main(int argc, char *argv[]) -> int {
   int const result = QGuiApplication::exec();
 
   // Explicitly destroy in correct order to prevent segfault
-  // Must cleanup OpenGL resources before destroying QML engine/window
   qInfo() << "Shutting down...";
   
-  // Clean up OpenGL resources while context is still valid
-  if (game_engine) {
-    game_engine->cleanupOpenGLResources();
-  }
-  
-  // Now safe to destroy QML engine (and OpenGL context)
+  // Destroy QML engine first (destroys OpenGL context)
   engine.reset();
   qInfo() << "QML engine destroyed";
   
-  // Then destroy game engine (non-OpenGL cleanup in destructor)
+  // Then destroy game engine
+  // OpenGL cleanup in destructors will be skipped if no valid context
   game_engine.reset();
   qInfo() << "GameEngine destroyed";
   

+ 10 - 0
render/gl/persistent_buffer.h

@@ -88,6 +88,16 @@ public:
       return;
     }
 
+    // Check if we have a valid OpenGL context before cleanup
+    if (QOpenGLContext::currentContext() == nullptr) {
+      // No valid context, just reset state without OpenGL calls
+      m_buffer = 0;
+      m_mappedPtr = nullptr;
+      m_capacity = 0;
+      m_totalSize = 0;
+      return;
+    }
+
     initializeOpenGLFunctions();
 
     if (m_mappedPtr != nullptr) {