- Cleaned up the message queue when a thread exit (but not the process).
- Modified QUEUE_DeleteMsgQueue to exit gracefully (without crashing Wine) if
  queue link list is corrupted.

diff --git a/windows/queue.c b/windows/queue.c
index fcaff97..9195e27 100644
--- a/windows/queue.c
+++ b/windows/queue.c
@@ -512,9 +512,18 @@
     while (*pPrev && (*pPrev != hQueue))
     {
         MESSAGEQUEUE *msgQ = (MESSAGEQUEUE*)GlobalLock16(*pPrev);
+
+        /* sanity check */
+        if ( !msgQ || (msgQ->magic != QUEUE_MAGIC) )
+        {
+            /* HQUEUE link list is corrupted, try to exit gracefully */
+            WARN( msg, "HQUEUE link list corrupted!\n");
+            pPrev = 0;
+            break;
+        }
         pPrev = &msgQ->next;
     }
-    if (*pPrev) *pPrev = msgQueue->next;
+    if (pPrev && *pPrev) *pPrev = msgQueue->next;
     msgQueue->self = 0;
 
     SYSTEM_UNLOCK();
@@ -579,7 +588,7 @@
 
 
 /***********************************************************************
- *           QUEUE_SetWakeBit               `
+ *           QUEUE_SetWakeBit
  *
  * See "Windows Internals", p.449
  */