Index: /OpenSceneGraph/trunk/include/osg/GLObjects
===================================================================
--- /OpenSceneGraph/trunk/include/osg/GLObjects (revision 7074)
+++ /OpenSceneGraph/trunk/include/osg/GLObjects (revision 7074)
@@ -0,0 +1,32 @@
+/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
+ *
+ * This library is open source and may be redistributed and/or modified under  
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * OpenSceneGraph Public License for more details.
+*/
+
+#ifndef OSG_GLOBJECTS
+#define OSG_GLOBJECTS 1
+
+#include <osg/Export>
+
+namespace osg {
+
+/** Flush all deleted OpenGL objects within the specified availableTime.
+  * Note, must be called from a thread which has current the graphics context associated with contextID. */
+extern OSG_EXPORT void flushDeletedGLObjects(unsigned int contextID, double currentTime, double& availableTime);
+
+/** Flush all deleted OpenGL objects.
+  * Note, must be called from a thread which has current the graphics context associated with contextID. */
+extern OSG_EXPORT void flushAllDeletedGLObjects(unsigned int contextID);
+
+
+}
+
+#endif
Index: /OpenSceneGraph/trunk/include/osgDB/DatabasePager
===================================================================
--- /OpenSceneGraph/trunk/include/osgDB/DatabasePager (revision 7017)
+++ /OpenSceneGraph/trunk/include/osgDB/DatabasePager (revision 7074)
@@ -219,12 +219,4 @@
         }
         
-        
-        /** Add a graphics context that should be used to compile/delete OpenGL objects.*/
-        void addCompileGraphicsContext(osg::GraphicsContext* gc);
-        
-        /** Removed a graphics context that should be used to compile/delete OpenGL objects.*/
-        void removeCompileGraphicsContext(osg::GraphicsContext* gc);
-
-        
         /** Turn the compilation of rendering objects for specfied graphics context on (true) or off(false). */
         void setCompileGLObjectsForContextID(unsigned int contextID, bool on);
@@ -232,5 +224,4 @@
         /** Get whether the compilation of rendering objects for specfied graphics context on (true) or off(false). */
         bool getCompileGLObjectsForContextID(unsigned int contextID);
-
 
         /** Rerturn true if an external draw thread should call compileGLObjects(..) or not.*/
@@ -376,5 +367,5 @@
 
         ActiveGraphicsContexts          _activeGraphicsContexts;
-        CompileGraphicsContexts         _compileGraphicsContexts;
+        // CompileGraphicsContexts         _compileGraphicsContexts;
         
         bool                            _doPreCompile;
Index: /OpenSceneGraph/trunk/applications/osgviewer/osgviewer.cpp
===================================================================
--- /OpenSceneGraph/trunk/applications/osgviewer/osgviewer.cpp (revision 7071)
+++ /OpenSceneGraph/trunk/applications/osgviewer/osgviewer.cpp (revision 7074)
@@ -158,6 +158,4 @@
         int processNum = 0;
 
-        osgDB::DatabasePager* dp = viewer.getScene()->getDatabasePager();
-
         for(unsigned int i=0; i<osg::GraphicsContext::getMaxContextID(); ++i)
         {
@@ -172,6 +170,4 @@
                 ++processNum;
             }
-
-            dp->addCompileGraphicsContext(gc);
         }
     }
Index: /OpenSceneGraph/trunk/src/osgUtil/SceneView.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgUtil/SceneView.cpp (revision 7062)
+++ /OpenSceneGraph/trunk/src/osgUtil/SceneView.cpp (revision 7074)
@@ -17,8 +17,7 @@
 #include <osg/Timer>
 #include <osg/GLExtensions>
+#include <osg/GLObjects>
 #include <osg/Notify>
 #include <osg/Texture>
-#include <osg/VertexProgram>
-#include <osg/FragmentProgram>
 #include <osg/AlphaFunc>
 #include <osg/TexEnv>
@@ -26,6 +25,4 @@
 #include <osg/LightModel>
 #include <osg/CollectOccludersVisitor>
-#include <osg/Shader>
-#include <osg/BufferObject>
 
 #include <osg/GLU>
@@ -836,37 +833,16 @@
     _requiresFlush = false;
     
-    double availableTime = 100.0f;
+    osg::flushAllDeletedGLObjects(getState()->getContextID());
+ }
+
+void SceneView::flushDeletedGLObjects(double& availableTime)
+{
+    osg::State* state = _renderInfo.getState();
+
+    _requiresFlush = false;
+
     double currentTime = state->getFrameStamp()?state->getFrameStamp()->getReferenceTime():0.0;
-    
-    osg::FrameBufferObject::flushDeletedFrameBufferObjects(state->getContextID(),currentTime,availableTime);
-    osg::RenderBuffer::flushDeletedRenderBuffers(state->getContextID(),currentTime,availableTime);
-    osg::Texture::flushAllDeletedTextureObjects(state->getContextID());
-    osg::Drawable::flushAllDeletedDisplayLists(state->getContextID());
-    osg::Drawable::flushDeletedVertexBufferObjects(state->getContextID(),currentTime,availableTime);
-    osg::VertexProgram::flushDeletedVertexProgramObjects(state->getContextID(),currentTime,availableTime);
-    osg::FragmentProgram::flushDeletedFragmentProgramObjects(state->getContextID(),currentTime,availableTime);
-    osg::Program::flushDeletedGlPrograms(state->getContextID(),currentTime,availableTime);
-    osg::Shader::flushDeletedGlShaders(state->getContextID(),currentTime,availableTime);
-    osg::BufferObject::flushDeletedBufferObjects(state->getContextID(),currentTime,availableTime);
- }
-
-void SceneView::flushDeletedGLObjects(double& availableTime)
-{
-    osg::State* state = _renderInfo.getState();
-
-    _requiresFlush = false;
-
-    double currentTime = state->getFrameStamp()?state->getFrameStamp()->getReferenceTime():0.0;
-
-    osg::FrameBufferObject::flushDeletedFrameBufferObjects(state->getContextID(),currentTime,availableTime);
-    osg::RenderBuffer::flushDeletedRenderBuffers(state->getContextID(),currentTime,availableTime);
-    osg::Texture::flushDeletedTextureObjects(state->getContextID(),currentTime,availableTime);
-    osg::Drawable::flushDeletedDisplayLists(state->getContextID(),availableTime);
-    osg::Drawable::flushDeletedVertexBufferObjects(state->getContextID(),currentTime,availableTime);
-    osg::VertexProgram::flushDeletedVertexProgramObjects(state->getContextID(),currentTime,availableTime);
-    osg::FragmentProgram::flushDeletedFragmentProgramObjects(state->getContextID(),currentTime,availableTime);
-    osg::Program::flushDeletedGlPrograms(state->getContextID(),currentTime,availableTime);
-    osg::Shader::flushDeletedGlShaders(state->getContextID(),currentTime,availableTime);
-    osg::BufferObject::flushDeletedBufferObjects(state->getContextID(),currentTime,availableTime);
+
+    osg::flushDeletedGLObjects(getState()->getContextID(), currentTime, availableTime);
 }
 
Index: /OpenSceneGraph/trunk/src/osg/GraphicsContext.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osg/GraphicsContext.cpp (revision 7073)
+++ /OpenSceneGraph/trunk/src/osg/GraphicsContext.cpp (revision 7074)
@@ -281,5 +281,5 @@
 GraphicsContext* GraphicsContext::getCompileContext(unsigned int contextID)
 {
-    osg::notify(osg::NOTICE)<<"GraphicsContext::getCompileContext "<<contextID<<std::endl;
+    //osg::notify(osg::NOTICE)<<"GraphicsContext::getCompileContext "<<contextID<<std::endl;
     OpenThreads::ScopedLock<OpenThreads::Mutex> lock(s_contextIDMapMutex);
     return s_contextIDMap[contextID]._compileContext.get();
Index: /OpenSceneGraph/trunk/src/osg/CMakeLists.txt
===================================================================
--- /OpenSceneGraph/trunk/src/osg/CMakeLists.txt (revision 7047)
+++ /OpenSceneGraph/trunk/src/osg/CMakeLists.txt (revision 7074)
@@ -60,4 +60,5 @@
     ${HEADER_PATH}/GL2Extensions
     ${HEADER_PATH}/GLExtensions
+    ${HEADER_PATH}/GLObjects
     ${HEADER_PATH}/GLU
     ${HEADER_PATH}/Geode
@@ -213,4 +214,5 @@
     FrontFace.cpp
     GLExtensions.cpp
+    GLObjects.cpp
     Geode.cpp
     Geometry.cpp
Index: /OpenSceneGraph/trunk/src/osg/GLObjects.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osg/GLObjects.cpp (revision 7074)
+++ /OpenSceneGraph/trunk/src/osg/GLObjects.cpp (revision 7074)
@@ -0,0 +1,52 @@
+/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2006 Robert Osfield 
+ *
+ * This library is open source and may be redistributed and/or modified under  
+ * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or 
+ * (at your option) any later version.  The full license is in LICENSE file
+ * included with this distribution, and on the openscenegraph.org website.
+ * 
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
+ * OpenSceneGraph Public License for more details.
+*/
+#include <osg/GLObjects>
+
+#include <osg/Texture>
+#include <osg/VertexProgram>
+#include <osg/FragmentProgram>
+#include <osg/Shader>
+#include <osg/BufferObject>
+#include <osg/FrameBufferObject>
+#include <osg/Drawable>
+
+void osg::flushDeletedGLObjects(unsigned int contextID, double currentTime, double& availableTime)
+{
+    osg::FrameBufferObject::flushDeletedFrameBufferObjects(contextID,currentTime,availableTime);
+    osg::RenderBuffer::flushDeletedRenderBuffers(contextID,currentTime,availableTime);
+    osg::Texture::flushDeletedTextureObjects(contextID,currentTime,availableTime);
+    osg::Drawable::flushDeletedDisplayLists(contextID,availableTime);
+    osg::Drawable::flushDeletedVertexBufferObjects(contextID,currentTime,availableTime);
+    osg::VertexProgram::flushDeletedVertexProgramObjects(contextID,currentTime,availableTime);
+    osg::FragmentProgram::flushDeletedFragmentProgramObjects(contextID,currentTime,availableTime);
+    osg::Program::flushDeletedGlPrograms(contextID,currentTime,availableTime);
+    osg::Shader::flushDeletedGlShaders(contextID,currentTime,availableTime);
+    osg::BufferObject::flushDeletedBufferObjects(contextID,currentTime,availableTime);
+}
+
+void osg::flushAllDeletedGLObjects(unsigned int contextID)
+{
+    double currentTime = DBL_MAX;
+    double availableTime = DBL_MAX;
+    osg::FrameBufferObject::flushDeletedFrameBufferObjects(contextID,currentTime,availableTime);
+    osg::RenderBuffer::flushDeletedRenderBuffers(contextID,currentTime,availableTime);
+    osg::Texture::flushAllDeletedTextureObjects(contextID);
+    osg::Drawable::flushAllDeletedDisplayLists(contextID);
+    osg::Drawable::flushDeletedVertexBufferObjects(contextID,currentTime,availableTime);
+    osg::VertexProgram::flushDeletedVertexProgramObjects(contextID,currentTime,availableTime);
+    osg::FragmentProgram::flushDeletedFragmentProgramObjects(contextID,currentTime,availableTime);
+    osg::Program::flushDeletedGlPrograms(contextID,currentTime,availableTime);
+    osg::Shader::flushDeletedGlShaders(contextID,currentTime,availableTime);
+    osg::BufferObject::flushDeletedBufferObjects(contextID,currentTime,availableTime);
+}
+
Index: /OpenSceneGraph/trunk/src/osgDB/DatabasePager.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgDB/DatabasePager.cpp (revision 7038)
+++ /OpenSceneGraph/trunk/src/osgDB/DatabasePager.cpp (revision 7074)
@@ -293,5 +293,4 @@
             startThread();
         }
-                
     }
 }
@@ -595,20 +594,21 @@
                 }
                 
-                if (loadedObjectsNeedToBeCompiled && !_compileGraphicsContexts.empty())
-                {
-                    for(CompileGraphicsContexts::iterator citr = _compileGraphicsContexts.begin();
-                        citr != _compileGraphicsContexts.end();
-                        ++citr)
+                if (loadedObjectsNeedToBeCompiled)
+                {
+                    for(ActiveGraphicsContexts::iterator itr = _activeGraphicsContexts.begin();
+                        itr != _activeGraphicsContexts.end();
+                        ++itr)
                     {
-                        osg::GraphicsContext* gc = citr->get();
+                        osg::GraphicsContext* gc = osg::GraphicsContext::getCompileContext(*itr);
                         if (gc)
                         {   
-                            osg::OperationsThread* gt = gc->getGraphicsThread();                            
-                            if (gt) gt->add(new DatabasePager::CompileOperation(this));
+                            osg::OperationsThread* gt = gc->getGraphicsThread();
+                            if (gt)
+                            {
+                                gt->add(new DatabasePager::CompileOperation(this));
+                            }
                             else
                             {
                                 gc->makeCurrent();
-                                
-                                // osg::notify(osg::NOTICE)<<"Database pager thread compiling"<<std::endl;
                                 
                                 compileAllGLObjects(*(gc->getState()));
@@ -618,4 +618,5 @@
                         }
                     }
+
                     // osg::notify(osg::NOTICE)<<"Done compiling in paging thread"<<std::endl;                   
                 }
@@ -884,35 +885,4 @@
 }
 
-void DatabasePager::addCompileGraphicsContext(osg::GraphicsContext* gc)
-{
-    for(CompileGraphicsContexts::iterator itr = _compileGraphicsContexts.begin();
-        itr != _compileGraphicsContexts.end();
-        ++itr)
-    {
-        if (*itr == gc)
-        {
-            return;
-        }
-    }
-    
-    _compileGraphicsContexts.push_back(gc);
-    
-    setCompileGLObjectsForContextID(gc->getState()->getContextID(),true);
-}
-
-void DatabasePager::removeCompileGraphicsContext(osg::GraphicsContext* gc)
-{
-    for(CompileGraphicsContexts::iterator itr = _compileGraphicsContexts.begin();
-        itr != _compileGraphicsContexts.end();
-        ++itr)
-    {
-        if (*itr == gc)
-        {
-            _compileGraphicsContexts.erase(itr);
-            return;
-        }
-    }
-}
-
 void DatabasePager::setCompileGLObjectsForContextID(unsigned int contextID, bool on)
 {
@@ -952,14 +922,6 @@
 {
     if (_activeGraphicsContexts.count(contextID)==0) return false;
-    
-    for(CompileGraphicsContexts::const_iterator citr = _compileGraphicsContexts.begin();
-        citr != _compileGraphicsContexts.end();
-        ++citr)
-    {
-        const osg::GraphicsContext* gc = citr->get();
-        if (gc && gc->getState()->getContextID()==contextID) return false;
-    }
-
-    return true;    
+
+    return osg::GraphicsContext::getCompileContext(contextID)==0;
 }
 
Index: /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowX11.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowX11.cpp (revision 7071)
+++ /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowX11.cpp (revision 7074)
@@ -19,4 +19,6 @@
 #include <osgViewer/api/X11/GraphicsWindowX11>
 #include <osgViewer/api/X11/PixelBufferX11>
+
+#include <osg/DeleteHandler>
 
 #include <X11/Xlib.h>
@@ -1179,4 +1181,10 @@
     ~X11WindowingSystemInterface()
     {
+        if (osg::Referenced::getDeleteHandler())
+        {
+            osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+            osg::Referenced::getDeleteHandler()->flushAll();
+        }
+
         //osg::notify(osg::NOTICE)<<"~X11WindowingSystemInterface()"<<std::endl;
         XSetErrorHandler(0);
@@ -1252,5 +1260,13 @@
     {
         osg::notify(osg::INFO)<<"~RegisterWindowingSystemInterfaceProxy()"<<std::endl;
+
+        if (osg::Referenced::getDeleteHandler())
+        {
+            osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+            osg::Referenced::getDeleteHandler()->flushAll();
+        }
+
         osg::GraphicsContext::setWindowingSystemInterface(0);
+
     }
 };
Index: /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowWin32.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowWin32.cpp (revision 7067)
+++ /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowWin32.cpp (revision 7074)
@@ -568,4 +568,10 @@
 Win32WindowingSystem::~Win32WindowingSystem()
 {
+    if (osg::Referenced::getDeleteHandler())
+    {
+        osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+        osg::Referenced::getDeleteHandler()->flushAll();
+    }
+    
     unregisterWindowClasses();
 }
@@ -2185,4 +2191,10 @@
     ~RegisterWindowingSystemInterfaceProxy()
     {
+        if (osg::Referenced::getDeleteHandler())
+        {
+            osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+            osg::Referenced::getDeleteHandler()->flushAll();
+        }
+
         osg::GraphicsContext::setWindowingSystemInterface(0);
     }
Index: /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp (revision 7066)
+++ /OpenSceneGraph/trunk/src/osgViewer/GraphicsWindowCarbon.cpp (revision 7074)
@@ -344,7 +344,13 @@
     
     /** dtor */
-    ~OSXCarbonWindowingSystemInterface() {
-        if (_displayIds)
-            delete[] _displayIds;
+    ~OSXCarbonWindowingSystemInterface()
+    {
+        if (osg::Referenced::getDeleteHandler())
+        {
+            osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+            osg::Referenced::getDeleteHandler()->flushAll();
+        }
+
+        if (_displayIds) delete[] _displayIds;
         _displayIds = NULL;
     }
@@ -1135,4 +1141,10 @@
     ~RegisterWindowingSystemInterfaceProxy()
     {
+        if (osg::Referenced::getDeleteHandler())
+        {
+            osg::Referenced::getDeleteHandler()->setNumFramesToRetainObjects(0);
+            osg::Referenced::getDeleteHandler()->flushAll();
+        }
+
         osg::GraphicsContext::setWindowingSystemInterface(0);
     }
Index: /OpenSceneGraph/trunk/src/osgWrappers/osgDB/DatabasePager.cpp
===================================================================
--- /OpenSceneGraph/trunk/src/osgWrappers/osgDB/DatabasePager.cpp (revision 7024)
+++ /OpenSceneGraph/trunk/src/osgWrappers/osgDB/DatabasePager.cpp (revision 7074)
@@ -260,14 +260,4 @@
 	          "Merge the changes to the scene graph by calling calling removeExpiredSubgraphs then addLoadedDataToSceneGraph. ",
 	          "Note, must only be called from single thread update phase. ");
-	I_Method1(void, addCompileGraphicsContext, IN, osg::GraphicsContext *, gc,
-	          Properties::NON_VIRTUAL,
-	          __void__addCompileGraphicsContext__osg_GraphicsContext_P1,
-	          "Add a graphics context that should be used to compile/delete OpenGL objects. ",
-	          "");
-	I_Method1(void, removeCompileGraphicsContext, IN, osg::GraphicsContext *, gc,
-	          Properties::NON_VIRTUAL,
-	          __void__removeCompileGraphicsContext__osg_GraphicsContext_P1,
-	          "Removed a graphics context that should be used to compile/delete OpenGL objects. ",
-	          "");
 	I_Method2(void, setCompileGLObjectsForContextID, IN, unsigned int, contextID, IN, bool, on,
 	          Properties::NON_VIRTUAL,
Index: /OpenSceneGraph/trunk/examples/osgteapot/osgteapot.cpp
===================================================================
--- /OpenSceneGraph/trunk/examples/osgteapot/osgteapot.cpp (revision 7053)
+++ /OpenSceneGraph/trunk/examples/osgteapot/osgteapot.cpp (revision 7074)
@@ -333,5 +333,5 @@
 int main(int , char **)
 {
-#if 1    
+#if 1
 
     // create viewer on heap as a test, this looks to be causing problems
@@ -345,5 +345,4 @@
     viewer->setSceneData( createTeapot() );
 
-    // create the windows and run the threads.
     return viewer->run();
     
@@ -358,5 +357,4 @@
     // create the windows and run the threads.
     return viewer.run();
-
 #endif
 
