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.
This commit is contained in:
tujinshu 2019-10-29 12:04:04 +08:00 committed by jp9000
parent cbfb779876
commit 76e6a99124

View File

@ -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();