From b7ec4ea16071d4d6d06c503c1890f4868e4f033f Mon Sep 17 00:00:00 2001 From: Maciej Suminski Date: Fri, 10 Jun 2016 17:07:12 +0200 Subject: [PATCH] Corrected the last instance check in OPENGL_GAL. --- common/gal/opengl/opengl_gal.cpp | 21 +++++++++++++++++---- include/gal/opengl/opengl_gal.h | 1 + 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index d6b5d444a1..4a6180cf93 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -51,6 +51,7 @@ static void InitTesselatorCallbacks( GLUtesselator* aTesselator ); static const int glAttributes[] = { WX_GL_RGBA, WX_GL_DOUBLEBUFFER, WX_GL_DEPTH_SIZE, 8, 0 }; wxGLContext* OPENGL_GAL::glMainContext = NULL; +int OPENGL_GAL::instanceCounter = 0; GLuint OPENGL_GAL::fontTexture = 0; bool OPENGL_GAL::isBitmapFontLoaded = false; SHADER* OPENGL_GAL::shader = NULL; @@ -74,6 +75,8 @@ OPENGL_GAL::OPENGL_GAL( wxWindow* aParent, wxEvtHandler* aMouseListener, glPrivContext = GL_CONTEXT_MANAGER::Get().CreateCtx( this, glMainContext ); } + ++instanceCounter; + // Check if OpenGL requirements are met runTest(); @@ -142,6 +145,7 @@ OPENGL_GAL::~OPENGL_GAL() { GL_CONTEXT_MANAGER::Get().LockCtx( glPrivContext ); + --instanceCounter; glFlush(); gluDeleteTess( tesselator ); ClearCache(); @@ -151,9 +155,18 @@ OPENGL_GAL::~OPENGL_GAL() delete nonCachedManager; delete overlayManager; + GL_CONTEXT_MANAGER::Get().UnlockCtx( glPrivContext ); + + // If it was the main context, then it will be deleted + // when the last OpenGL GAL instance is destroyed (a few lines below) + if( glPrivContext != glMainContext ) + GL_CONTEXT_MANAGER::Get().DestroyCtx( glPrivContext ); + // Are destroying the last GAL instance? - if( glPrivContext == glMainContext ) + if( instanceCounter == 0 ) { + GL_CONTEXT_MANAGER::Get().LockCtx( glMainContext ); + if( isBitmapFontLoaded ) { glDeleteTextures( 1, &fontTexture ); @@ -161,11 +174,11 @@ OPENGL_GAL::~OPENGL_GAL() } delete shader; - glMainContext = NULL; + + GL_CONTEXT_MANAGER::Get().UnlockCtx( glMainContext ); + GL_CONTEXT_MANAGER::Get().DestroyCtx( glMainContext ); } - GL_CONTEXT_MANAGER::Get().UnlockCtx( glPrivContext ); - GL_CONTEXT_MANAGER::Get().DestroyCtx( glPrivContext ); } diff --git a/include/gal/opengl/opengl_gal.h b/include/gal/opengl/opengl_gal.h index 508e13a6e5..8e218c7281 100644 --- a/include/gal/opengl/opengl_gal.h +++ b/include/gal/opengl/opengl_gal.h @@ -275,6 +275,7 @@ private: static wxGLContext* glMainContext; ///< Parent OpenGL context wxGLContext* glPrivContext; ///< Canvas-specific OpenGL context + static int instanceCounter; ///< GL GAL instance counter wxEvtHandler* mouseListener; wxEvtHandler* paintListener;