Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 39015)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/context.c	(revision 39016)
@@ -846,9 +846,30 @@
 #ifdef VBOX_WITH_WDDM
         , IWineD3DSwapChainImpl *swapchain
-#endif
+# ifdef DEBUG_misha
+        , BOOL fExpectedValid
+# endif
+#endif
+
         )
 {
 #ifdef VBOX_WITH_WDDM
-    if (!swapchain || context->currentSwapchain == swapchain)
+    if (!swapchain)
+    {
+        swapchain = context->currentSwapchain;
+    }
+
+    if (!swapchain)
+    {
+        context->valid = FALSE;
+# ifdef DEBUG_misha
+        if (fExpectedValid)
+        {
+            ERR("no current swapchain!\n");
+        }
+# endif
+        return;
+    }
+
+    if (swapchain == context->currentSwapchain)
     {
         context->valid = swapchain_validate(context->currentSwapchain);
@@ -882,5 +903,9 @@
     IWineD3DSwapChainImpl *swapchain = NULL;
 
-    context_validate(context, NULL);
+    context_validate(context, NULL
+# ifdef DEBUG_misha
+                , FALSE
+# endif
+            );
     if (context->valid)
         return;
@@ -889,5 +914,9 @@
     if (swapchain)
     {
-        context_validate(context, swapchain);
+        context_validate(context, swapchain
+# ifdef DEBUG_misha
+                , TRUE
+# endif
+                );
         if (!context->valid)
         {
@@ -1790,5 +1819,9 @@
         IWineD3DSwapChain *swapchain = NULL;
         if (target && SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
-            context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
+            context_validate(context, (IWineD3DSwapChainImpl*)swapchain
+# ifdef DEBUG_misha
+                , TRUE
+# endif
+                    );
             IWineD3DSwapChain_Release(swapchain);
         }
@@ -2348,5 +2381,9 @@
         IWineD3DSwapChain *swapchain = NULL;
         if (SUCCEEDED(IWineD3DSurface_GetContainer(target, &IID_IWineD3DSwapChain, (void **)&swapchain))) {
-            context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
+            context_validate(context, (IWineD3DSwapChainImpl*)swapchain
+# ifdef DEBUG_misha
+                , TRUE
+# endif
+                    );
             IWineD3DSwapChain_Release(swapchain);
         }
@@ -2361,5 +2398,9 @@
 
         context = findThreadContextForSwapChain(swapchain);
-        context_validate(context, (IWineD3DSwapChainImpl*)swapchain);
+        context_validate(context, (IWineD3DSwapChainImpl*)swapchain
+# ifdef DEBUG_misha
+                , TRUE
+# endif
+                );
         IWineD3DSwapChain_Release(swapchain);
     }
@@ -2376,5 +2417,9 @@
                 swapchain = (IWineD3DSwapChainImpl *)This->swapchains[This->NumberOfSwapChains-1]; /* just fallback to anything to avoid NPE */
             context = findThreadContextForSwapChain((IWineD3DSwapChain*)swapchain);
-            context_validate(context, swapchain);
+            context_validate(context, swapchain
+# ifdef DEBUG_misha
+                , TRUE
+# endif
+                    );
         }
 
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 39015)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/surface.c	(revision 39016)
@@ -1249,6 +1249,23 @@
     if (!ref)
     {
+#ifdef VBOX_WITH_WDDM
+        IWineD3DDeviceImpl *device = This->resource.device;
+        struct wined3d_context *context;
+        UINT i;
+#endif
         surface_cleanup(This);
         This->resource.parent_ops->wined3d_object_destroyed(This->resource.parent);
+
+#ifdef VBOX_WITH_WDDM
+        for (i = 0; i < device->numContexts; ++i)
+        {
+            context = device->contexts[i];
+            /* pretty hacky, @todo: check if the context is acquired and re-acquire it with the new swapchain */
+            if (context->current_rt  == This)
+            {
+                context->current_rt = NULL;
+            }
+        }
+#endif
 
         TRACE("(%p) Released.\n", This);
Index: /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c
===================================================================
--- /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 39015)
+++ /trunk/src/VBox/Additions/WINNT/Graphics/Wine/wined3d/swapchain.c	(revision 39016)
@@ -49,5 +49,5 @@
 IWineD3DSwapChainImpl * swapchain_find(IWineD3DDeviceImpl *pDevice, HWND hWnd)
 {
-    int i;
+    UINT i;
     for (i = 0; i < pDevice->NumberOfSwapChains; ++i)
     {
@@ -64,4 +64,35 @@
 VOID swapchain_invalidate(IWineD3DSwapChainImpl *pSwapchain)
 {
+    /* first make sure the swapchain is not used by anyone */
+    IWineD3DDeviceImpl *device = pSwapchain->device;
+    struct wined3d_context *context;
+    UINT i;
+    for (i = 0; i < device->numContexts; ++i)
+    {
+        context = device->contexts[i];
+        /* pretty hacky, @todo: check if the context is acquired and re-acquire it with the new swapchain */
+        if (context->currentSwapchain == pSwapchain)
+        {
+            context->currentSwapchain = NULL;
+        }
+
+        if (pSwapchain->frontBuffer == context->current_rt)
+        {
+            context->current_rt = NULL;
+        }
+        else if (pSwapchain->backBuffer)
+        {
+            UINT j;
+            for (j = 0; j < pSwapchain->presentParms.BackBufferCount; ++j)
+            {
+                if (pSwapchain->backBuffer[j] == context->current_rt)
+                {
+                    context->current_rt = NULL;
+                    break;
+                }
+            }
+        }
+    }
+
     pSwapchain->win_handle = NULL;
     pSwapchain->hDC = NULL;
@@ -96,5 +127,5 @@
     if (This->backBuffer)
     {
-        UINT i = This->presentParms.BackBufferCount;
+        i = This->presentParms.BackBufferCount;
 
         while (i--)
