Fixed crash in IoCompletionPort during unregistration. Also remove WSACleanup() from pj_shutdown() since it may not be a good idea (and for one thing it crashes with Invalid Handle exception)



git-svn-id: https://svn.pjsip.org/repos/pjproject/trunk@821 74dad513-b988-da41-8d7b-12977e46ad98
diff --git a/pjlib/src/pj/ioqueue_winnt.c b/pjlib/src/pj/ioqueue_winnt.c
index 71977cb..ae6bb70 100644
--- a/pjlib/src/pj/ioqueue_winnt.c
+++ b/pjlib/src/pj/ioqueue_winnt.c
@@ -687,6 +687,11 @@
 	pj_lock_release(ioqueue->lock);
     }
 #endif
+
+#if PJ_IOQUEUE_HAS_SAFE_UNREG
+    /* Mark key as closing before closing handle. */
+    key->closing = 1;
+#endif
     
     /* Close handle (the only way to disassociate handle from IOCP). 
      * We also need to close handle to make sure that no further events
@@ -701,12 +706,6 @@
     key->cb.on_write_complete = NULL;
 
 #if PJ_IOQUEUE_HAS_SAFE_UNREG
-    /* Mark key as closing. */
-    key->closing = 1;
-
-    /* Decrement reference counter. */
-    decrement_counter(key);
-
     /* Even after handle is closed, I suspect that IOCP may still try to
      * do something with the handle, causing memory corruption when pool
      * debugging is enabled.
@@ -714,7 +713,11 @@
      * Forcing context switch seems to have fixed that, but this is quite
      * an ugly solution..
      */
-    pj_thread_sleep(0);
+    while (pj_atomic_get(key->ref_count) != 1)
+	pj_thread_sleep(0);
+
+    /* Decrement reference counter to destroy the key. */
+    decrement_counter(key);
 #endif
 
     return PJ_SUCCESS;
diff --git a/pjlib/src/pj/os_core_win32.c b/pjlib/src/pj/os_core_win32.c
index ea2c8d9..23e2cde 100644
--- a/pjlib/src/pj/os_core_win32.c
+++ b/pjlib/src/pj/os_core_win32.c
@@ -219,7 +219,7 @@
     }
 
     /* Shutdown Winsock */
-    WSACleanup();
+    //WSACleanup();
 }