In our allocator, we store a thread local heap ptr to access it, and this appears to work fine
if (!thread_local_ex_mimalloc_heap)
{
thread_local_ex_mimalloc_heap = mi_heap_new_in_arena(m_arena_id);
}
... // later when allocating etc
rtn = mi_heap_malloc_aligned(thread_local_ex_mimalloc_heap, size, alignment);
If we use MI_SKIP_COLLECT_ON_EXIT a simple test program that generates N threads, make a load of allocations and deallocations behaves completely as expected, but if we enable mi_collect to run on exit, then on shutdown we have a segfault when pages are 'cleaned up' (?)
Now if we look at the value of th segment pointer, it points to our custom arena memory allocation (in this case segment points to 0x7fffd4000000 which from printf and inspection, I know is the base address of our custom memory arena allocation
If we simply do not delete our own custom arena memory on shutdown (munmap), the segfault goes away - because now the seegment is a valid address and all is well. We can also enable MI_SKIP_COLLECT_ON_EXIT to hide the problem - but it seems to me that this is a bug in mimalloc - segments from custom arenas, should not be moved to the primary heap at any time for cleanup operations - they should remain owned and managed only in the heaps bound to those arenas.
Have I misunderstood whats going on, and can we fix our code some other way. Thanks.
We have a custom arena which needs to be thread safe, we allocate memory (with
mmap
) and useIn our allocator, we store a
thread local
heap ptr to access it, and this appears to work fineIf we use MI_SKIP_COLLECT_ON_EXIT a simple test program that generates N threads, make a load of allocations and deallocations behaves completely as expected, but if we enable
mi_collect
to run on exit, then on shutdown we have a segfault when pages are 'cleaned up' (?)The error is triggered in
segment.c
whenmi_abandoned_pop
tries to get the 'next' segment from the abandoned list(?)Now if we look at the value of th segment pointer, it points to our custom arena memory allocation (in this case segment points to
0x7fffd4000000
which from printf and inspection, I know is the base address of our custom memory arena allocationIf we simply do not delete our own custom arena memory on shutdown (
munmap
), the segfault goes away - because now the seegment is a valid address and all is well. We can also enableMI_SKIP_COLLECT_ON_EXIT
to hide the problem - but it seems to me that this is a bug in mimalloc - segments from custom arenas, should not be moved to the primary heap at any time for cleanup operations - they should remain owned and managed only in the heaps bound to those arenas.Have I misunderstood whats going on, and can we fix our code some other way. Thanks.