Skip to content

Commit 08b64e7

Browse files
committed
use subproc_main for final statistics output to avoid dereferencing the heap
1 parent 7c7ecf0 commit 08b64e7

File tree

4 files changed

+7
-4
lines changed

4 files changed

+7
-4
lines changed

include/mimalloc/internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ void _mi_heap_page_reclaim(mi_heap_t* heap, mi_page_t* page);
239239
// "stats.c"
240240
void _mi_stats_init(void);
241241
void _mi_stats_done(mi_stats_t* stats);
242+
void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out, void* arg) mi_attr_noexcept;
242243
void _mi_stats_merge_thread(mi_tld_t* tld);
243244
void _mi_stats_merge_from(mi_stats_t* to, mi_stats_t* from);
244245
mi_msecs_t _mi_clock_now(void);

src/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -784,7 +784,7 @@ void mi_cdecl _mi_process_done(void) {
784784
//_mi_page_map_unsafe_destroy(_mi_subproc_main());
785785

786786
if (mi_option_is_enabled(mi_option_show_stats) || mi_option_is_enabled(mi_option_verbose)) {
787-
mi_stats_print(NULL);
787+
_mi_stats_print(&_mi_subproc_main()->stats, NULL, NULL); // use always main subproc at process exit to avoid dereferencing the heap (as it may be destroyed by now)
788788
}
789789
_mi_allocator_done();
790790
_mi_verbose_message("process done: 0x%zx\n", tld_main.thread_id);

src/options.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,10 @@ void mi_register_output(mi_output_fun* out, void* arg) mi_attr_noexcept {
405405
// add stderr to the delayed output after the module is loaded
406406
static void mi_add_stderr_output(void) {
407407
mi_assert_internal(mi_out_default == NULL);
408-
mi_out_buf_flush(&mi_out_stderr, false, NULL); // flush current contents to stderr
409-
mi_out_default = &mi_out_buf_stderr; // and add stderr to the delayed output
408+
if (mi_out_default==NULL) {
409+
mi_out_buf_flush(&mi_out_stderr, false, NULL); // flush current contents to stderr
410+
mi_out_default = &mi_out_buf_stderr; // and add stderr to the delayed output
411+
}
410412
}
411413

412414
// --------------------------------------------------------

src/stats.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ static void mi_cdecl mi_buffered_out(const char* msg, void* arg) {
310310
// Print statistics
311311
//------------------------------------------------------------
312312

313-
static void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0) mi_attr_noexcept {
313+
void _mi_stats_print(mi_stats_t* stats, mi_output_fun* out0, void* arg0) mi_attr_noexcept {
314314
// wrap the output function to be line buffered
315315
char buf[256]; _mi_memzero_var(buf);
316316
buffered_t buffer = { out0, arg0, NULL, 0, 255 };

0 commit comments

Comments
 (0)