Skip to content

glue: re-bind shared GL context before destroying shared GL objects (windows) - #38

Closed
igagis-bot wants to merge 4 commits into
cppfw:mainfrom
igagis-bot:fix-windows-gl-exit
Closed

igagis-bot wants to merge 4 commits into
cppfw:mainfrom
igagis-bot:fix-windows-gl-exit

Conversation

@igagis-bot

Copy link
Copy Markdown
Contributor

Fixes a crash on exit (ESC / quit) of OpenGL apps: GL_INVALID_OPERATION in opengl_buffer::~opengl_buffer() debug assertions (exit code 127).

Root cause

On exit, application_glue members are destroyed in reverse declaration order: the windows (and their GL contexts) are destroyed first, leaving no current GL context on the thread. The shared GL objects — common shaders, common render objects (unit-quad VBO/index buffers), and shared resource textures in the style provider / resource loader — are destroyed after the windows, so their GL delete calls ran with no current context. The shared GL context (whose HGLRC these objects belong to) still existed but was no longer current.

Fix

Add a guard member (restore_shared_gl_context_on_destruction) to application_glue, declared after all owners of shared GL objects so it is destroyed before them; its destructor re-binds the shared GL context (which outlives the shared objects) on the current thread.

Verification

  • Reproduced the crash via the app probe server (run{quit}) before the fix: GL_INVALID_OPERATION (1282), wglGetCurrentContext() == NULL, exit code 127.
  • After the fix: clean exit, code 0, no GL errors.
  • Full rebuild clean; all test suites pass (337 + 59 + 557 + 329 + 353).

…windows)

On exit, the windows (and their GL contexts) are destroyed before the
shared GL objects (common shaders, common render objects, shared
resource textures) still owned by the glue, so those GL objects were
deleted with no current GL context on the thread, triggering
GL_INVALID_OPERATION in the debug GL error assertions.

Add a guard member to application_glue that is destroyed right before
the shared GL objects and re-binds the shared GL context (which
outlives them) on this thread.
# Conflicts:
#	src/ruisapp/glue/windows/application.hxx
Main now removes the shared gl context window in the Vulkan variant,
so guard the shared gl context restorer the same way. Vulkan has no
'current context' to re-bind.

Also, declare the restorer before ruis_style_provider to satisfy
-Wreorder, and format the code with clang-format.
@igagis igagis self-assigned this Nov 26, 2026
@igagis

igagis commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

It is better to do the other approach. the ruis::render::factory should have protected virtual bind(){} method (vulkan impl will not override it leaving it empty). then concrete factories can override it to bind the factory's native rendering context. the ruis::render::context shoukld be a friend of the ruis::render::factory. when ruis::render::context is destroyed it checks if the context_Stack is empty, if not it binds the top context, if it is empty, then it will call this->factory->bind(). then it shoulkd fix it for all?

@igagis-bot

Copy link
Copy Markdown
Contributor Author

Implemented as suggested, in the render layer instead of the glue:

Yes, this fixes it for all glue implementations (windows, xorg, wayland, ...) at once, since the shared render objects are now destroyed with a valid current context on every platform. Verified locally on Windows: both the opengl and opengles test apps now exit cleanly on window close (previously the opengl one crashed with GL_INVALID_OPERATION during teardown).

Closing this PR as obsolete — the glue-level guard is removed, the branch now matches main.

@igagis-bot igagis-bot closed this Nov 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants