On 2026-09-21 Pali Rohár wrote:
> On Monday 21 September 2026 18:09:26 Lasse Collin wrote:
> > 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().
Yes, the docs require that the "object is unowned":
The caller is responsible for ensuring that the critical section
object is unowned and the specified CRITICAL_SECTION structure is
not being accessed by any critical section functions called by
other threads in the process.
https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-deletecriticalsection
> 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.
Correct. The only issue could be unwanted messages from leak detection
tools.
> > 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.
Yes.
> 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 see lpvReserved documented here:
https://learn.microsoft.com/en-us/windows/win32/dlls/dllmain
That looks like a working solution. :-)
In gdtoa/misc.c, freeing freelist[] and p5s should be added. In the
default configuration (when Omit_Private_Memory is undefined),
freelist[] can contain a mix of statically-allocated and
dynamically-allocated pointers. To avoid distinguishing between two
kinds of pointers, maybe Omit_Private_Memory would be fine to define.
Then the cleanup could be something like this (untested):
for (size_t k = 0; k <= Kmax; k++) {
Bigint *b = freelist[k];
while (b != NULL) {
Bigint *tmp = b;
b = b->next;
free(tmp);
}
}
while (p5s != NULL) {
Bigint *tmp = p5s;
p5s = p5s->next;
free(tmp);
}
--
Lasse Collin
_______________________________________________
Mingw-w64-public mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/mingw-w64-public