microsoft / microsoft/mimalloc
Heaps and C++ static destructors
Nobody has claimed this yet.
- Dominant language
- C
- Stars
- 13.4k
- Forks
- 1.2k
- Avg merge
- 4d 45m
- Merged PRs (30d)
- 13
Description
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?
Contributor guide
No contributing guide indexed for this repository
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the Windows debug-mode scenario with mimalloc statically linked into a DLL, exported heap creation and destruction wrappers, and a statically instantiated executable object. Trace FLS cleanup alongside the object's destructor; done means the heap is not destroyed twice and process exit completes without a crash.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- c
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100