wined3d: Non-primary stateblocks hold an internal reference on indexbuffers.
diff --git a/dlls/wined3d/stateblock.c b/dlls/wined3d/stateblock.c
index 0362053..5b50e0f 100644
--- a/dlls/wined3d/stateblock.c
+++ b/dlls/wined3d/stateblock.c
@@ -262,7 +262,6 @@
                     }
                 }
             }
-            if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
         }
 
         for (counter = 0; counter < MAX_STREAMS; counter++) {
@@ -272,6 +271,7 @@
                 }
             }
         }
+        if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
 
         for(counter = 0; counter < LIGHTMAP_SIZE; counter++) {
             struct list *e1, *e2;
@@ -488,7 +488,9 @@
         if (This->changed.indices && ((This->pIndexData != targetStateBlock->pIndexData)
                         || (This->baseVertexIndex != targetStateBlock->baseVertexIndex))) {
             TRACE("Updating pindexData to %p, baseVertexIndex to %d\n",
-            targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
+                  targetStateBlock->pIndexData, targetStateBlock->baseVertexIndex);
+            if(targetStateBlock->pIndexData) IWineD3DIndexBuffer_AddRef(targetStateBlock->pIndexData);
+            if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
             This->pIndexData = targetStateBlock->pIndexData;
             This->baseVertexIndex = targetStateBlock->baseVertexIndex;
         }
@@ -604,7 +606,6 @@
         memcpy(This->streamOffset, targetStateBlock->streamOffset, sizeof(This->streamOffset));
         memcpy(This->streamFreq, targetStateBlock->streamFreq, sizeof(This->streamFreq));
         memcpy(This->streamFlags, targetStateBlock->streamFlags, sizeof(This->streamFlags));
-        This->pIndexData = targetStateBlock->pIndexData;
         This->baseVertexIndex = targetStateBlock->baseVertexIndex;
         memcpy(This->transforms, targetStateBlock->transforms, sizeof(This->transforms));
         record_lights(This, targetStateBlock);
@@ -623,6 +624,11 @@
         memcpy(This->samplerState, targetStateBlock->samplerState, sizeof(This->samplerState));
         This->scissorRect = targetStateBlock->scissorRect;
 
+        if(targetStateBlock->pIndexData != This->pIndexData) {
+            if(targetStateBlock->pIndexData) IWineD3DIndexBuffer_AddRef(targetStateBlock->pIndexData);
+            if(This->pIndexData) IWineD3DIndexBuffer_Release(This->pIndexData);
+            This->pIndexData = targetStateBlock->pIndexData;
+        }
         for(i = 0; i < MAX_STREAMS; i++) {
             if(targetStateBlock->streamSource[i] != This->streamSource[i]) {
                 if(targetStateBlock->streamSource[i]) IWineD3DVertexBuffer_AddRef(targetStateBlock->streamSource[i]);