Fix TLS index and virtual reservation leaks on DLL unload#1261
Closed
jinpzhanAMD wants to merge 2 commits intomicrosoft:dev3from
Closed
Fix TLS index and virtual reservation leaks on DLL unload#1261jinpzhanAMD wants to merge 2 commits intomicrosoft:dev3from
jinpzhanAMD wants to merge 2 commits intomicrosoft:dev3from
Conversation
added 2 commits
April 14, 2026 15:37
Free the raw TLS slots and destroy the main subprocess arenas during process teardown so DLL unload releases allocator state cleanly.
daanx
added a commit
that referenced
this pull request
Apr 14, 2026
Collaborator
|
Thank you so much @jinpzhanAMD for the PR -- I added your suggestions in the latest |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
When mimalloc is statically linked into a DLL that is repeatedly loaded/unloaded, Application Verifier reports two resource leaks on DLL_PROCESS_DETACH. This PR fixes both.
Changes
mi_win_tls_slot_alloc() calls TlsAlloc() twice (for _mi_theap_default_slot and _mi_theap_cached_slot) but TlsFree() is never called on DLL unload.
Fix: Save the raw DWORD returned by TlsAlloc() into mi_tls_raw_default / mi_tls_raw_cached, and free them via mi_win_tls_slots_done() called from mi_process_done(). This avoids trying to reverse-engineer the original TLS index from mimalloc's internal slot representation, which is fragile and error-prone (e.g. expansion slots would compute wrong indices).
mi_subproc_unsafe_destroy() only calls _mi_arenas_unsafe_destroy_all() for non-main subprocesses (guarded by if (subproc != &subproc_main)). When mi_subprocs_unsafe_destroy_all() destroys the main subproc, its arena virtual memory reservations (VirtualAlloc(MEM_RESERVE)) are never released via VirtualFree.
Fix: Add _mi_arenas_unsafe_destroy_all(&subproc_main) after mi_subproc_unsafe_destroy(&subproc_main) in mi_subprocs_unsafe_destroy_all().
Set mi_option_enable(mi_option_destroy_on_exit) in mi_process_init() so arena and page-map cleanup runs automatically on process/DLL shutdown, which is required for the DLL unload scenario.
Testing
Verified with Windows Application Verifier (leak checks enabled) on a DLL that statically links mimalloc. Both 0x350 and 0x903 verifier stops are resolved after these changes.