diff --git a/components/sceneutil/glextensions.cpp b/components/sceneutil/glextensions.cpp index 078af90c3c..eb7783c45f 100644 --- a/components/sceneutil/glextensions.cpp +++ b/components/sceneutil/glextensions.cpp @@ -4,16 +4,29 @@ namespace SceneUtil { namespace { - osg::observer_ptr sGLExtensions; + std::set> sGLExtensions; + + class GLExtensionsObserver : public osg::Observer + { + public: + static GLExtensionsObserver sInstance; + + void objectDeleted(void* referenced) override + { + sGLExtensions.erase(static_cast(referenced)); + } + }; + + GLExtensionsObserver GLExtensionsObserver::sInstance{}; } osg::GLExtensions& getGLExtensions() { - if (!sGLExtensions) + if (sGLExtensions.empty()) throw std::runtime_error( "GetGLExtensionsOperation was not used when the current context was created or there is no current " "context"); - return *sGLExtensions; + return **sGLExtensions.begin(); } GetGLExtensionsOperation::GetGLExtensionsOperation() @@ -23,6 +36,7 @@ namespace SceneUtil void GetGLExtensionsOperation::operator()(osg::GraphicsContext* graphicsContext) { - sGLExtensions = graphicsContext->getState()->get(); + auto [itr, _] = sGLExtensions.emplace(graphicsContext->getState()->get()); + (*itr)->addObserver(&GLExtensionsObserver::sInstance); } }