changeset 1987:21811c9e2eaf

OS/2: Fix memory and HAB/HMQ leak when compiled with Watcom C. OS/2 and Win: Fix resource leak when calling dw_thread_end().
author bsmith@81767d24-ef19-dc11-ae90-00e081727c95
date Sun, 15 Sep 2019 23:02:16 +0000
parents 218fce0e3b06
children 197f9463efa8
files os2/dw.c win/dw.c
diffstat 2 files changed, 23 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/os2/dw.c	Sat Sep 07 16:47:45 2019 +0000
+++ b/os2/dw.c	Sun Sep 15 23:02:16 2019 +0000
@@ -2,7 +2,7 @@
  * Dynamic Windows:
  *          A GTK like implementation of the PM GUI
  *
- * (C) 2000-2017 Brian Smith <brian@dbsoft.org>
+ * (C) 2000-2019 Brian Smith <brian@dbsoft.org>
  * (C) 2003-2011 Mark Hessling <mark@rexx.org>
  * (C) 2000 Achim Hasenmueller <achimha@innotek.de>
  * (C) 2000 Peter Nielsen <peter@pmview.com>
@@ -11927,16 +11927,16 @@
  * However it is exported so language bindings can call it when
  * they create threads that require access to Dynamic Windows.
  */
-void API _dw_init_thread(void)
+void **_dw_init_thread2(void)
 {
    HAB thishab = WinInitialize(0);
    HMQ thishmq = WinCreateMsgQueue(thishab, 0);
-#ifndef __WATCOMC__
    void **threadinfo = (void **)malloc(sizeof(void *) * 2);
 
    threadinfo[0] = (void *)thishab;
    threadinfo[1] = (void *)thishmq;
    
+#ifndef __WATCOMC__
    *_threadstore() = (void *)threadinfo;
 #endif
 
@@ -11944,6 +11944,12 @@
    /* Set the codepage to 1208 (UTF-8) */
    WinSetCp(thishmq, 1208);
 #endif
+   return threadinfo;
+}
+
+void API _dw_init_thread(void)
+{
+   _dw_init_thread2();
 }
 
 /* 
@@ -11952,11 +11958,13 @@
  * However it is exported so language bindings can call it when
  * they exit threads that require access to Dynamic Windows.
  */
-void API _dw_deinit_thread(void)
+void _dw_deinit_thread2(void **threadinfo)
 {
 #ifndef __WATCOMC__
-   void **threadinfo = (void **)*_threadstore();
-   
+   if(!threadinfo)
+      threadinfo = (void **)*_threadstore();
+#endif
+	
    if(threadinfo)
    {
       HAB thishab = (HAB)threadinfo[0];
@@ -11966,7 +11974,11 @@
       WinTerminate(thishab);
       free(threadinfo);
    }
-#endif
+}
+
+void API _dw_deinit_thread(void)
+{
+   _dw_deinit_thread2(NULL);
 }
 
 /*
@@ -11976,15 +11988,14 @@
 {
    void (API_FUNC threadfunc)(void *) = NULL;
    void **tmp = (void **)data;
-
-   _dw_init_thread();
+   void **threadinfo = _dw_init_thread2();
 
    threadfunc = (void (API_FUNC)(void *))tmp[0];
    threadfunc(tmp[1]);
 
    free(tmp);
 
-   _dw_deinit_thread();
+   _dw_deinit_thread2(threadinfo);
 }
 
 /*
@@ -12016,6 +12027,7 @@
  */
 void API dw_thread_end(void)
 {
+   _dw_deinit_thread();
    _endthread();
 }
 
--- a/win/dw.c	Sat Sep 07 16:47:45 2019 +0000
+++ b/win/dw.c	Sun Sep 15 23:02:16 2019 +0000
@@ -11870,6 +11870,7 @@
 void API dw_thread_end(void)
 {
 #if !defined(__CYGWIN__)
+   _dw_deinit_thread();
    _endthread();
 #endif
 }