From 76e6a99124945013aff46ccb3e1d6486c93cc6fb Mon Sep 17 00:00:00 2001 From: tujinshu <517208148@qq.com> Date: Tue, 29 Oct 2019 12:04:04 +0800 Subject: [PATCH] win-capture: Fix rare crash when GL program exits **Commit message modified and clarified by Jim** When hooking a program that has both DirectX and OpenGL contexts in use, it is possible to cause a crash on shutdown due to capture_active() returning true when an OpenGL context is deleted. Normally, when capturing an OpenGL program, this would not happen because the 'active' variable would not be set due to OpenGL capture not being initialized, but if DirectX is captured while an OpenGL context is available, and OpenGL could not load these required functions, then GL can crash due to trying to use unavailable functions. This case is extremely rare and doesn't happen under normal circumstances; only if a program is using both DirectX and OpenGL within the same program simultaneously, and *only* if OpenGL could not load the required functions. This likely almost never happens under normal programs, games, and hardware. This was apparently produced by hooking a GL Qt program that used QWebEngine, which used multiple contexts at once. --- plugins/win-capture/graphics-hook/gl-capture.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/win-capture/graphics-hook/gl-capture.c b/plugins/win-capture/graphics-hook/gl-capture.c index 206a50d0d..5d6db11a9 100644 --- a/plugins/win-capture/graphics-hook/gl-capture.c +++ b/plugins/win-capture/graphics-hook/gl-capture.c @@ -30,6 +30,7 @@ static struct func_hook wgl_swap_buffers; static struct func_hook wgl_delete_context; static bool darkest_dungeon_fix = false; +static bool functions_initialized = false; struct gl_data { HDC hdc; @@ -724,7 +725,6 @@ static void gl_shmem_capture(void) static void gl_capture(HDC hdc) { - static bool functions_initialized = false; static bool critical_failure = false; if (critical_failure) { @@ -828,7 +828,7 @@ static BOOL WINAPI hook_wgl_delete_context(HGLRC hrc) { BOOL ret; - if (capture_active()) { + if (capture_active() && functions_initialized) { HDC last_hdc = jimglGetCurrentDC(); HGLRC last_hrc = jimglGetCurrentContext();