diff --git a/src/osg/GraphicsContext.cpp b/src/osg/GraphicsContext.cpp index c7c3e3944..d131b4c37 100644 --- a/src/osg/GraphicsContext.cpp +++ b/src/osg/GraphicsContext.cpp @@ -547,6 +547,9 @@ void GraphicsContext::close(bool callCloseImplementation) } + bool contextIDValid = _state.valid(); + unsigned int contextID = _state.valid() ? _state->getContextID() : ~0U; + if (callCloseImplementation && _state.valid() && isRealized()) { OSG_INFO<<"Closing still viable window "<getContextID()="<<_state->getContextID()<getContextID()); + _state->reset(); + + // Kill the State now so all the (directly or indirectly) referenced GLObjects can make it into delete cache in time. + // That way osg::deleteAllGLObjects won't miss them and left them to be deleted afterwards in a new GraphicsContext + // that happens to have the same contextID (see SceneView::draw() and _requiresFlush flag). + _state = 0; + + osg::deleteAllGLObjects(contextID); OSG_INFO<<"Done delete of GL objects"<getContextID()); - } - _state->reset(); + _state->reset(); + } releaseContext(); } @@ -585,18 +595,18 @@ void GraphicsContext::close(bool callCloseImplementation) // now discard any deleted deleted OpenGL objects that the are still hanging around - such as due to // the the flushDelete*() methods not being invoked, such as when using GraphicContextEmbedded where makeCurrent // does not work. - if ( !sharedContextExists && _state.valid()) + if ( !sharedContextExists && contextIDValid) { OSG_INFO<<"Doing discard of deleted OpenGL objects."<getContextID()); + osg::discardAllGLObjects(contextID); } - if (_state.valid()) + if (contextIDValid) { - decrementContextIDUsageCount(_state->getContextID()); + decrementContextIDUsageCount(contextID); - _state = 0; + if (_state.valid()) _state = 0; } }