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.
> > 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.
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).
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(). Even in DLLs it shouldn't be run
when the main thread exits while other threads are still active.
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