From 47050371a1eb935a1571c1e17c3b142402ccc24e Mon Sep 17 00:00:00 2001 From: Daan Leijen Date: Mon, 22 Feb 2021 15:05:47 -0800 Subject: [PATCH] fix issue #363 and disable assertion for now --- src/init.c | 5 ++++- src/segment.c | 3 +-- test/main-override.cpp | 27 +++++++++++++++++++++------ 3 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/init.c b/src/init.c index aee08f5a..ecb73d6c 100644 --- a/src/init.c +++ b/src/init.c @@ -265,7 +265,10 @@ static bool _mi_heap_done(mi_heap_t* heap) { // free if not the main thread if (heap != &_mi_heap_main) { - mi_assert_internal(heap->tld->segments.count == 0 || heap->thread_id != _mi_thread_id()); + // the following assertion does not always hold for huge segments as those are always treated + // as abondened: one may allocate it in one thread, but deallocate in another in which case + // the count can be too large or negative. todo: perhaps not count huge segments? see issue #363 + // mi_assert_internal(heap->tld->segments.count == 0 || heap->thread_id != _mi_thread_id()); _mi_os_free(heap, sizeof(mi_thread_data_t), &_mi_stats_main); } #if 0 diff --git a/src/segment.c b/src/segment.c index d2902e69..acb7c58d 100644 --- a/src/segment.c +++ b/src/segment.c @@ -1311,7 +1311,7 @@ static mi_page_t* mi_segment_huge_page_alloc(size_t size, mi_segments_tld_t* tld mi_segment_t* segment = mi_segment_alloc(size,tld,os_tld,&page); if (segment == NULL || page==NULL) return NULL; mi_assert_internal(segment->used==1); - mi_assert_internal(mi_page_block_size(page) >= size); + mi_assert_internal(mi_page_block_size(page) >= size); segment->thread_id = 0; // huge segments are immediately abandoned return page; } @@ -1334,7 +1334,6 @@ void _mi_segment_huge_page_free(mi_segment_t* segment, mi_page_t* page, mi_block page->is_zero = false; mi_assert(page->used == 0); mi_tld_t* tld = heap->tld; - // mi_segments_track_size((long)segment->segment_size, tld); _mi_segment_page_free(page, true, &tld->segments); } #if (MI_DEBUG!=0) diff --git a/test/main-override.cpp b/test/main-override.cpp index fe5403d1..4acdb34e 100644 --- a/test/main-override.cpp +++ b/test/main-override.cpp @@ -32,14 +32,17 @@ void heap_late_free(); // issue #204 void padding_shrink(); // issue #209 void various_tests(); void test_mt_shutdown(); +void large_alloc(void); // issue #363 int main() { mi_stats_reset(); // ignore earlier allocations - heap_thread_free_large(); - heap_no_delete(); - heap_late_free(); - padding_shrink(); - various_tests(); + large_alloc(); + + //heap_thread_free_large(); + //heap_no_delete(); + //heap_late_free(); + //padding_shrink(); + //various_tests(); //test_mt_shutdown(); mi_stats_print(NULL); return 0; @@ -176,7 +179,7 @@ void heap_thread_free_large_worker() { void heap_thread_free_large() { for (int i = 0; i < 100; i++) { - shared_p = mi_malloc_aligned(2*1024*1024 + 1, 8); + shared_p = mi_malloc_aligned(2 * 1024 * 1024 + 1, 8); auto t1 = std::thread(heap_thread_free_large_worker); t1.join(); } @@ -207,3 +210,15 @@ void test_mt_shutdown() std::cout << "done" << std::endl; } + +// issue #363 +using namespace std; + +void large_alloc(void) +{ + char* a = new char[1ull << 25]; + thread th([&] { + delete[] a; + }); + th.join(); +} \ No newline at end of file