AFAIK mingw-w64 uses callbacks to reclaim TLS memory. In the first case, upon destruction of the static object there were 5 blocks of memory unfreed; and in the second case there were 6. If we say there was no memory leak in case 1, then there must be in case 2, IMO.

I've tried this myself, and I think I'm seeing what you are seeing. It looks to me like the problem comes from ___w64_mingwthr_add_key_dtor in tlsthrd.c. In this routine, calloc is called to create a __mingwthr_key_t. However, I don't believe that memory ever gets freed.

In theory it could get freed in ___w64_mingwthr_remove_key_dtor (at least there is a free for it there), but apparently that routine never gets called.

Maybe it was supposed to be freed in __mingwthr_run_key_dtors when it gets called?

So, I believe I have the patch for this (attached). lh_mouse, can you confirm?

Note that the way you were checking your code (test2.cpp) is flawed, since some of the frees occur after the destructor of your counter. As an alternative, try adding putchar('+') in malloc and putchar('-') in free, and count the pluses and minuses in the output.

Turns out it's not much of a leak, and the memory can't be freed until application shutdown (or possibly at dll unload time) since it is used right up until the last minute.

dw
>From e9adda2479244cbf3681c379b2573362eace6308 Mon Sep 17 00:00:00 2001
From: David Wohlferd <d...@limegreensocks.com>
Date: Fri, 31 Oct 2014 20:00:04 -0700
Subject: [PATCH] Fix memory leak.

Signed-off-by: David Wohlferd <d...@limegreensocks.com>
---
 mingw-w64-crt/crt/tlsthrd.c | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/mingw-w64-crt/crt/tlsthrd.c b/mingw-w64-crt/crt/tlsthrd.c
index 5d858a6..c367ca1 100644
--- a/mingw-w64-crt/crt/tlsthrd.c
+++ b/mingw-w64-crt/crt/tlsthrd.c
@@ -133,6 +133,14 @@ __mingw_TLScallback (HANDLE __UNUSED_PARAM(hDllHandle),
       __mingwthr_run_key_dtors();
       if (__mingwthr_cs_init == 1)
         {
+          __mingwthr_key_t volatile *keyp, *t;
+          for (keyp = key_dtor_list; keyp; )
+          {
+            t = keyp->next;
+            free((void *)keyp);
+            keyp = t;
+          }
+          key_dtor_list = NULL;
           __mingwthr_cs_init = 0;
           DeleteCriticalSection (&__mingwthr_cs);
         }
-- 
1.9.4.msysgit.0

------------------------------------------------------------------------------
_______________________________________________
Mingw-w64-public mailing list
Mingw-w64-public@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public

Reply via email to