{"record":{"id":"c09200bc0d12fc0e","repo":"MonoGame/MonoGame","slug":"could-not-terminate-egl-connection","errorCode":null,"errorMessage":"Could not terminate EGL connection","messagePattern":"Could not terminate EGL connection","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"MonoGame.Framework/Platform/Android/MonoGameAndroidGameView.cs","lineNumber":766,"sourceCode":"\n        protected void EnsureUndisposed()\n        {\n            if (disposed)\n                throw new ObjectDisposedException(\"\");\n        }\n\n        protected void DestroyGLContext()\n        {\n            if (eglContext != null)\n            {\n                if (!egl.EglDestroyContext(eglDisplay, eglContext))\n                    throw new Exception(\"Could not destroy EGL context\" + GetErrorAsString());\n                eglContext = null;\n            }\n            if (eglDisplay != null)\n            {\n                if (!egl.EglTerminate(eglDisplay))\n                    throw new Exception(\"Could not terminate EGL connection\" + GetErrorAsString());\n                eglDisplay = null;\n            }\n\n            glContextAvailable = false;\n        }\n\n        protected void DestroyGLSurface()\n        {\n            if (!(eglSurface == null || eglSurface == IEGL10.EglNoSurface))\n            {\n                if (!egl.EglMakeCurrent(eglDisplay, IEGL10.EglNoSurface,\n                        IEGL10.EglNoSurface, IEGL10.EglNoContext))\n                {\n                    Log.Verbose(\"AndroidGameView\", \"Could not unbind EGL surface\" + GetErrorAsString());\n                }\n\n                if (!egl.EglDestroySurface(eglDisplay, eglSurface))\n                {","sourceCodeStart":748,"sourceCodeEnd":784,"githubUrl":"https://github.com/MonoGame/MonoGame/blob/1d71bbd0ff4071a03caa583aa3bc56b85924a819/MonoGame.Framework/Platform/Android/MonoGameAndroidGameView.cs#L748-L784","documentation":"Thrown by MonoGameAndroidGameView.DestroyGLContext when egl.EglTerminate(eglDisplay) returns false, meaning the EGL connection could not be terminated. The message is appended with GetErrorAsString() for the EGL error code. This occurs during the second phase of context teardown (after the context itself), when terminating the EGL display connection fails.","triggerScenarios":"DestroyGLContext proceeds to egl.EglTerminate after destroying the context, but the display connection is already invalid, terminated, or there are outstanding contexts/surfaces preventing termination.","commonSituations":"Teardown ordering issues (other contexts still referencing the display), GPU process crash invalidating the display, or calling DestroyGLContext after the display was already terminated elsewhere.","solutions":["Ensure all contexts/surfaces are destroyed before terminating the display connection.","Make EGL resources non-current before teardown to release references.","Wrap teardown in try/catch so a transient EGL terminate failure does not crash on background/exit.","On GPU-process loss, recreate the EGL display/connection from scratch."],"exampleFix":"// before\nif (!egl.EglTerminate(eglDisplay))\n    throw new Exception(\"Could not terminate EGL connection\" + GetErrorAsString());\neglDisplay = null;\n// after\nif (!egl.EglTerminate(eglDisplay))\n    Debug.WriteLine(\"EGL terminate failed: \" + GetErrorAsString());\neglDisplay = null;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { /* eglTerminate in DestroyGLContext */ }\ncatch (Exception ex)\n{\n    Debug.WriteLine(\"EGL terminate failed: \" + ex.Message);\n}","preventionTips":["Destroy all contexts/surfaces before terminating the display.","Make EGL resources non-current first to release references.","Treat teardown as best-effort to avoid crashes on background."],"tags":["android","egl","opengl","graphics-context","teardown","driver"],"backgroundTag":null,"analyzedSha":"1d71bbd0ff4071a03caa583aa3bc56b85924a819","analyzedAt":"2026-08-13T17:10:33.625Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}