Index: /trunk/src/VBox/GuestHost/OpenGL/include/cr_bits.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/cr_bits.h	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/cr_bits.h	(revision 41057)
@@ -65,4 +65,48 @@
 }
 
+static INLINE int CHECKBIT( const unsigned int *b, const unsigned int bit )
+{
+    unsigned int node32 = bit >> 5;
+    unsigned int node = bit & 0x1f;
+
+    return !!(b[node32] & (1 < node));
+}
+
+static INLINE void CLEARBIT( unsigned int *b, const unsigned int bit )
+{
+    unsigned int node32 = bit >> 5;
+    unsigned int node = bit & 0x1f;
+
+    b[node32] &=  ~(1 << node);
+}
+
+static INLINE void SETBIT( unsigned int *b, const unsigned int bit )
+{
+    unsigned int node32 = bit >> 5;
+    unsigned int node = bit & 0x1f;
+
+    b[node32] |=  (1 << node);
+}
+
+static INLINE int HASBITS( const unsigned int *b )
+{
+    int j;
+
+    for (j=0;j<CR_MAX_BITARRAY;j++)
+        if (b[j])
+            return 1;
+
+    return 0;
+}
+
+static INLINE void CLEARBITS( unsigned int *b )
+{
+    int j;
+
+    for (j=0;j<CR_MAX_BITARRAY;j++)
+        b[j] = 0;
+}
+
+
 #ifdef __cplusplus
 }
Index: /trunk/src/VBox/GuestHost/OpenGL/include/cr_glstate.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/cr_glstate.h	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/cr_glstate.h	(revision 41057)
@@ -116,4 +116,8 @@
 } CRSharedState;
 
+#define CR_STATE_SHAREDOBJ_USAGE_INIT(_pObj) (CLEARBITS((_pObj)->ctxUsage))
+#define CR_STATE_SHAREDOBJ_USAGE_SET(_pObj, _pCtx) (SETBIT((_pObj)->ctxUsage, (_pCtx)->id))
+#define CR_STATE_SHAREDOBJ_USAGE_CLEAR(_pObj, _pCtx) (CLEARBIT((_pObj)->ctxUsage, (_pCtx)->id))
+#define CR_STATE_SHAREDOBJ_USAGE_IS_USED(_pObj) (HASBITS((_pObj)->ctxUsage))
 
 /**
@@ -231,5 +235,5 @@
 typedef FNCRSTATE_CONTEXT_GET *PFNCRSTATE_CONTEXT_GET;
 DECLEXPORT(int32_t) crStateLoadContext(CRContext *pContext, CRHashTable * pCtxTable, PFNCRSTATE_CONTEXT_GET pfnCtxGet, PSSMHANDLE pSSM);
-DECLEXPORT(void)    crStateFreeShared(CRSharedState *s);
+DECLEXPORT(void)    crStateFreeShared(CRContext *pContext, CRSharedState *s);
 #endif
 
Index: /trunk/src/VBox/GuestHost/OpenGL/include/cr_server.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/cr_server.h	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/cr_server.h	(revision 41057)
@@ -31,5 +31,5 @@
 #endif
 
-#define SHCROGL_SSM_VERSION 28
+#define SHCROGL_SSM_VERSION 29
 
 #define CR_MAX_WINDOWS 100
Index: /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_bufferobject.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_bufferobject.h	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_bufferobject.h	(revision 41057)
@@ -42,4 +42,8 @@
 	CRbitvalue dirty[CR_MAX_BITARRAY];  /* dirty data or state */
 	GLintptrARB dirtyStart, dirtyLength; /* dirty region */
+#ifndef IN_GUEST
+    /* bitfield representing the object usage. 1 means the object is used by the context with the given bitid */
+    CRbitvalue             ctxUsage[CR_MAX_BITARRAY];
+#endif
 } CRBufferObject;
 
Index: /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_framebuffer.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_framebuffer.h	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_framebuffer.h	(revision 41057)
@@ -49,4 +49,7 @@
 #ifdef IN_GUEST
     GLenum                  status;
+#else
+    /* bitfield representing the object usage. 1 means the object is used by the context with the given bitid */
+    CRbitvalue             ctxUsage[CR_MAX_BITARRAY];
 #endif
 } CRFramebufferObject;
@@ -57,4 +60,8 @@
     GLenum   internalformat;
     GLuint   redBits, greenBits, blueBits, alphaBits, depthBits, stencilBits;
+#ifndef IN_GUEST
+    /* bitfield representing the object usage. 1 means the object is used by the context with the given bitid */
+    CRbitvalue             ctxUsage[CR_MAX_BITARRAY];
+#endif
 } CRRenderbufferObject;
 
Index: /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_texture.h
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_texture.h	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/include/state/cr_texture.h	(revision 41057)
@@ -97,4 +97,8 @@
     CRbitvalue             imageBit[CR_MAX_BITARRAY];
     CRbitvalue             paramsBit[CR_MAX_TEXTURE_UNITS][CR_MAX_BITARRAY];
+#ifndef IN_GUEST
+    /* bitfield representing the object usage. 1 means the object is used by the context with the given bitid */
+    CRbitvalue             ctxUsage[CR_MAX_BITARRAY];
+#endif
 } CRTextureObj;
 
Index: /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_bufferobject.c	(revision 41057)
@@ -23,4 +23,7 @@
         b->access = GL_READ_WRITE_ARB;
         b->bResyncOnRead = GL_FALSE;
+#ifndef IN_GUEST
+        CR_STATE_SHAREDOBJ_USAGE_INIT(b);
+#endif
     }
     return b;
@@ -189,4 +192,8 @@
             crHashtableAdd( g->shared->buffersTable, buffer, newObj );
         }
+
+#ifndef IN_GUEST
+        CR_STATE_SHAREDOBJ_USAGE_SET(newObj, g);
+#endif
     }
 
Index: /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_framebuffer.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_framebuffer.c	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_framebuffer.c	(revision 41057)
@@ -98,5 +98,12 @@
             fbo->renderbuffer->internalformat = GL_RGBA;
             crHashtableAdd(g->shared->rbTable, renderbuffer, fbo->renderbuffer);
-        }
+#ifndef IN_GUEST
+        CR_STATE_SHAREDOBJ_USAGE_INIT(fbo->renderbuffer);
+#endif
+        }
+#ifndef IN_GUEST
+        CR_STATE_SHAREDOBJ_USAGE_SET(fbo->renderbuffer, g);
+#endif
+
     }
     else fbo->renderbuffer = NULL;
@@ -295,5 +302,12 @@
             crStateInitFrameBuffer(pFBO);
             crHashtableAdd(g->shared->fbTable, framebuffer, pFBO);
-        }
+#ifndef IN_GUEST
+            CR_STATE_SHAREDOBJ_USAGE_INIT(pFBO);
+#endif
+        }
+
+#ifndef IN_GUEST
+        CR_STATE_SHAREDOBJ_USAGE_SET(pFBO, g);
+#endif
     }
 
@@ -458,4 +472,8 @@
     CRSTATE_FBO_CHECKERR(textarget!=GL_TEXTURE_1D, GL_INVALID_OPERATION, "textarget");
 
+#ifndef IN_GUEST
+    CR_STATE_SHAREDOBJ_USAGE_SET(tobj, g);
+#endif
+
     crStateInitFBOAttachmentPoint(ap);
     ap->type = GL_TEXTURE;
@@ -483,4 +501,8 @@
 
     CRSTATE_FBO_CHECKERR(GL_TEXTURE_1D==textarget || GL_TEXTURE_3D==textarget, GL_INVALID_OPERATION, "textarget");
+
+#ifndef IN_GUEST
+    CR_STATE_SHAREDOBJ_USAGE_SET(tobj, g);
+#endif
 
     crStateInitFBOAttachmentPoint(ap);
@@ -514,4 +536,8 @@
     CRSTATE_FBO_CHECKERR(zoffset>(g->limits.max3DTextureSize-1), GL_INVALID_VALUE, "zoffset too big");
     CRSTATE_FBO_CHECKERR(textarget!=GL_TEXTURE_3D, GL_INVALID_OPERATION, "textarget");
+
+#ifndef IN_GUEST
+    CR_STATE_SHAREDOBJ_USAGE_SET(tobj, g);
+#endif
 
     crStateInitFBOAttachmentPoint(ap);
Index: /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_init.c	(revision 41057)
@@ -61,9 +61,52 @@
 }
 
+#ifndef IN_GUEST
+typedef struct CR_STATE_RELEASEOBJ
+{
+    CRContext *pCtx;
+    CRSharedState *s;
+} CR_STATE_RELEASEOBJ, *PCR_STATE_RELEASEOBJ;
+
+static void ReleaseTextureCallback(unsigned long key, void *data1, void *data2)
+{
+    PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
+    CRTextureObj *pObj = (CRTextureObj *)data1;
+    CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
+    if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
+        crHashtableDelete(pData->s->textureTable, key, DeleteTextureCallback);
+}
+
+static void ReleaseBufferObjectCallback(unsigned long key, void *data1, void *data2)
+{
+    PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
+    CRBufferObject *pObj = (CRBufferObject *)data1;
+    CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
+    if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
+        crHashtableDelete(pData->s->buffersTable, key, crStateFreeBufferObject);
+}
+
+static void ReleaseFBOCallback(unsigned long key, void *data1, void *data2)
+{
+    PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
+    CRFramebufferObject *pObj = (CRFramebufferObject *)data1;
+    CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
+    if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
+        crHashtableDelete(pData->s->fbTable, key, crStateFreeFBO);
+}
+
+static void ReleaseRBOCallback(unsigned long key, void *data1, void *data2)
+{
+    PCR_STATE_RELEASEOBJ pData = (PCR_STATE_RELEASEOBJ)data2;
+    CRRenderbufferObject *pObj = (CRRenderbufferObject *)data1;
+    CR_STATE_SHAREDOBJ_USAGE_CLEAR(pObj, pData->pCtx);
+    if (!CR_STATE_SHAREDOBJ_USAGE_IS_USED(pObj))
+        crHashtableDelete(pData->s->rbTable, key, crStateFreeRBO);
+}
+#endif
 /**
  * Decrement shared state's refcount and delete when it hits zero.
  */
 DECLEXPORT(void)
-crStateFreeShared(CRSharedState *s)
+crStateFreeShared(CRContext *pContext, CRSharedState *s)
 {
     s->refCount--;
@@ -80,4 +123,17 @@
         crFree(s);
     }
+#ifndef IN_GUEST
+    else
+    {
+        /* evaluate usage bits*/
+        CR_STATE_RELEASEOBJ CbData;
+        CbData.pCtx = pContext;
+        CbData.s = s;
+        crHashtableWalk(s->textureTable, ReleaseTextureCallback, &CbData);
+        crHashtableWalk(s->buffersTable, ReleaseBufferObjectCallback , &CbData);
+        crHashtableWalk(s->fbTable, ReleaseFBOCallback, &CbData);
+        crHashtableWalk(s->rbTable, ReleaseRBOCallback, &CbData);
+    }
+#endif
 }
 
@@ -103,5 +159,5 @@
         else
         {
-            crStateFreeShared(pCtx->shared);
+            crStateFreeShared(pCtx, pCtx->shared);
             pCtx->shared = gSharedState;
             gSharedState->refCount++;
@@ -125,5 +181,5 @@
             pCtx->shared = crStateAllocShared();
             pCtx->shared->id = pCtx->id;
-            crStateFreeShared(gSharedState);
+            crStateFreeShared(pCtx, gSharedState);
         }
     }
@@ -285,5 +341,5 @@
     crStateTextureDestroy( ctx );
     crStateTransformDestroy( ctx );
-    crStateFreeShared(ctx->shared);
+    crStateFreeShared(ctx, ctx->shared);
     crStateFramebufferObjectDestroy(ctx);
     crStateGLSLDestroy(ctx);
Index: /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_snapshot.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_snapshot.c	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_snapshot.c	(revision 41057)
@@ -1498,5 +1498,5 @@
             CRASSERT(pContext->shared->refCount==1);
             bLoadShared = GL_FALSE;
-            crStateFreeShared(pContext->shared);
+            crStateFreeShared(pContext, pContext->shared);
             pContext->shared = NULL;
             pTmpContext->shared->refCount++;
Index: /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texture.c
===================================================================
--- /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texture.c	(revision 41056)
+++ /trunk/src/VBox/GuestHost/OpenGL/state_tracker/state_texture.c	(revision 41057)
@@ -250,4 +250,9 @@
         RESET(tobj->paramsBit[i], ctx->bitid);
     }
+
+#ifndef IN_GUEST
+    CR_STATE_SHAREDOBJ_USAGE_INIT(tobj);
+    CR_STATE_SHAREDOBJ_USAGE_SET(tobj, ctx);
+#endif
 }
 
@@ -901,4 +906,8 @@
         tobj = crStateTextureAllocate_t(g, texture);
     }
+
+#ifndef IN_GUEST
+    CR_STATE_SHAREDOBJ_USAGE_SET(tobj, g);
+#endif
 
     /* Check the targets */
