[vlc-devel] [PATCH] win32: only call WSACleanup if WSAStartup worked

Steve Lhomme robux4 at ycbcr.xyz
Fri Mar 27 08:55:34 CET 2020


According to the documentation:
There must be a call to WSACleanup for each successful call to WSAStartup. Only
the final WSACleanup function call performs the actual cleanup.
---
 src/win32/specific.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/win32/specific.c b/src/win32/specific.c
index cf5836af0b6..75e0f7f5c51 100644
--- a/src/win32/specific.c
+++ b/src/win32/specific.c
@@ -36,6 +36,8 @@
 #include <mmsystem.h>
 #include <winsock2.h>
 
+static bool WSA_initialized = false;
+
 static int system_InitWSA(int hi, int lo)
 {
     WSADATA data;
@@ -43,10 +45,14 @@ static int system_InitWSA(int hi, int lo)
     if (WSAStartup(MAKEWORD(hi, lo), &data) == 0)
     {
         if (LOBYTE(data.wVersion) == 2 && HIBYTE(data.wVersion) == 2)
+        {
+            WSA_initialized = true;
             return 0;
+        }
         /* Winsock DLL is not usable */
         WSACleanup( );
     }
+    WSA_initialized = false;
     return -1;
 }
 
@@ -173,6 +179,6 @@ void system_Configure( libvlc_int_t *p_this, int i_argc, const char *const ppsz_
  */
 void system_End(void)
 {
-    /* XXX: In theory, we should not call this if WSAStartup() failed. */
-    WSACleanup();
+    if (WSA_initialized)
+        WSACleanup();
 }
-- 
2.17.1



More information about the vlc-devel mailing list