microsoft / microsoft/mimalloc
Heaps and C++ static destructors
Nessuno ha ancora preso questa issue.
- Lingua principale
- C
- Stelle
- 13.4k
- Fork
- 1.2k
- Merge medio
- 4g 45m
- PR unite (30g)
- 13
Descrizione
Hi,
the following scenario:
- Windows
- debug mode
- mimalloc statically built and linked into a (single) DLL
Now, I'm wrapping some mimalloc functions (including those for heap creation/destruction), and those wrapper functions are exported from the DLL and used by an executable.
The wrappers are called from the constructor and destructor of a class, instanced statically in the executable.
Now, when exiting the executable I get a crash when a heap is destroyed by that static instance: the heap object is already destroyed (filled with 0xfd)!
AFAICS this happens because the "FLS cleanup" is happening quite early during exiting, with the destruction of the static instance not yet having happened.
As the FLS cleanup destroys all heaps for the thread this also destroys the heap held in the static instance, and at it's destruction time, it's essentially a heap "double destroy".
I briefly tried the same, but with a dynamically linked mimalloc. This works fine, it appears the "thread detach" cleanup happens late enough here.
My workaround is (currently) to not destroy the heap in the destructor of the class that is statically instanced.
However, this is probably not a general solution...
I guess one approach to remedy this issue could be to perform the "thread cleanup" for the main thread as late as possible (essentially, during process cleanup). Though I wonder whether that would have other implications I may not see...
Any thoughts on the matter?
Guida per i contributori
Nessuna guida per i contributori indicizzata per questo repository
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia riproducendo lo scenario Windows in modalità debug, con mimalloc collegato staticamente in una DLL, wrapper esportati per la creazione e la distruzione dell’heap e un oggetto eseguibile istanziato staticamente. Traccia la pulizia di FLS insieme al distruttore dell’oggetto; il lavoro è completato quando l’heap non viene distrutto due volte e il processo termina senza un crash.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- c
- Ambito
- operating-systems
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Da chiarire
- Idoneità per principianti
- 25/100