On Monday 21 September 2026 18:09:26 Lasse Collin wrote:
> On 2026-09-21 LIU Hao wrote:
> > 在 2026-9-21 19:15, Pali Rohár 写道:
> > > So... If I understand correctly, this problem happens because the
> > > __attribute__((destructor)) functions are called from the main
> > > thread during exiting the process and at the same time are still
> > > running other threads, right?
>
> Correct. The problem exists with both the old atexit() method and the
> new __attribute__((destructor)) method.
Ok, thank you for confirmation.
> > > I would try to think how to address this issue. Maybe I come up with
> > > something.
> > >
> >
> > IIRC the discussion on IRC is that the destructor can be executed
> > while other threads are running so they access a destroyed
> > `CRITICAL_SECTION`.
>
> In addition, DestroyCriticalSection() might be called while another
> thread is holding the lock.
Is this a problem? I mean if it is a problem to call DestroyCriticalSection()
when other thread will never call LeaveCriticalSection().
> As I understand it, there's no way to do the cleanup when an
> application exits: The other threads might never terminate, so the
> main thread cannot wait for them. One cannot force-kill the threads and
> then do the cleanup, because the threads might hold the
> CRITICAL_SECTIONS in misc.c, and also misc.c calls malloc() and free()
> which may do locking too (force-killing might leave such locks locked).
I come to the same conclusion. dtoa_lock() and dtoa_unlock() cannot use
dtoa_CritSec array if the dtoa_CS_init is in state 3. But current code
if (2 == dtoa_CS_init)
EnterCriticalSection(&dtoa_CritSec[n]);
and
if (2 == dtoa_CS_init)
LeaveCriticalSection (&dtoa_CritSec[n]);
contains race condition - after the dtoa_CS_init check, other thread can
call dtoa_lock_cleanup() which changes dtoa_CS_init to 3 and delete the
content of dtoa_CritSec array. Fixing this would need another critical
section and opens a new problem who and when will delete it.
So IMHO it is not to do the cleanup when application is exiting.
And moreover at the time when application is already exiting, I think it
is not required to free all resources. crtexe.c already contains some
allocations (e.g. argv[] array) which are never released.
> Cleanup should be possible if the code is unloaded with FreeLibrary(),
> because unloading shouldn't be done when any thread uses the code. So
> the cleanup function should only be run in DLLs that are unloaded with
> an explicit call to FreeLibrary().
Yes. But here it not just should be possible, but it is required to
release resources. FreeLibrary() explicitly unloads library together
with its all resources. Not doing it will cause the real memory leak.
> Even in DLLs it shouldn't be run
> when the main thread exits while other threads are still active.
Maybe more precious. If the application is exiting, the
DeleteCriticalSection() call in dtoa_lock_cleanup() must not be called.
And it applies for all loaded modules (main EXE and all DLLs).
I was doing cleanup of the crtdll.c code and we should be able in
_CRT_INIT() function for DLL_PROCESS_DETACH event call some callback
with information if the DLL is being unloaded by FreeLibrary or by the
fact that whole process is exiting (this is stored in lpreserved
parameter - even it is called "reserved", MS documents it that is not
reserved and contains this information).
> I'm sure the freelist[] cache exists for a very good reason, but I
> wonder how much performance benefit there is with the most common
> Windows CRTs. If the value wasn't high in mingw-w64's use, not using
> the cache would avoid some of the leaks. I didn't try to benchmark it.
>
> The other locking and caching is in pow5mult(). The lock protects the
> pointer p5s. I didn't look in detail how that code works.
>
> Side note 1: In the error condition, pow5mult() will "return NULL;"
> without releasing the lock (same also with p51 later in the function):
>
> ACQUIRE_DTOA_LOCK(1);
> if (!(p5 = p5s)) {
> p5 = p5s = i2b(625);
> if (p5 == NULL)
> return NULL;
> p5->next = 0;
> }
> FREE_DTOA_LOCK(1);
>
> Side note 2: Balloc may return NULL if MALLOC fails. Some functions
> check the return value of Balloc, but it's not perfect. For example,
> lshift() in misc.c returns NULL if Balloc() fails, but callers of
> lshift() don't check for NULL.
>
> gdtoaimp.h says that MALLOC should be defined so that it doesn't return
> if allocation fails. So defining MALLOC to a wrapper that calls abort()
> on malloc() failure might be a hacky "fix" to both side notes above.
> That's not acceptable behavior in stdio functions, but at least it
> would be a more controlled crash compared to the current code.
>
> --
> Lasse Collin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public